Nhibernate ICriteria - property length check

I am trying to return all objects where this property is not empty. The problem IsNotEmpty()applies only to collections. Below is the general approach that I have done so far, it clearly does not work.

ICriteria lvCriteria = NHibernateHelper.GetCurrentSession()
                                       .CreateCriteria(typeof(FunctionCall))
                                       .SetMaxResults(100)
                                       .AddOrder(Order.Desc("LogId"));

if (pvMsg.HasValue)
{                
       lvCriteria.Add(Restrictions.IsNotNull("Msg"))
                 .Add(Restrictions.IsNotEmpty("Msg"));
}

Any suggestions? Is it possible to achieve this result by checking the length of the property value? Thank!

+3
source share
4 answers

Finally, I discovered the combination I was looking for!

lvCriteria.Add(Restrictions.Not(Expression.Eq("Msg", string.Empty)));

This combination of constraints and expressions works as expected; narrowing all blank lines. I don’t know why I couldn’t achieve these results even with:

lvCriteria.Add(Restrictions.Not(Restrictions.Eq("Msg", string.Empty)));

Thanks to everyone who tried.

+4
source

I believe what you are looking for:

.Add(Expression.IsNotEmpty("PropertyName"));

and

.Add(Expression.IsNotNull("PropertyName"));
0
source

:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Domain" namespace="Assembly.Domain">
  <class name="Assembly.Domain.FunctionCall, Domain" lazy="false" table="FunctionCallLog">

    <id name="LogId" column="LogId">
      <generator class="native" />
    </id>

    <property name="LogTime" column="LogTime" />
    <property name="Username" column="Username" />
    <property name="CallerIp" column="CallerIp" />
    <property name="FunctionName" column="FunctionName" />
    <property name="Parameters" column="Parameters" />
    <property name="Msg" column="Msg" />
    <property name="FileName" column="FileName" />
    <property name="TimeSpan" column="TimeSpan" />

  </class>
</hibernate-mapping>


using System;

namespace Assembly.Domain
{
    public class FunctionCall
    {
        public int LogId { get; set; }
        public DateTime LogTime { get; set; }
        public string Username { get; set; }
        public string CallerIp { get; set; }
        public string FunctionName { get; set; }
        public string Parameters { get; set; }
        public string Msg { get; set; }
        public string FileName { get; set; }
        public int TimeSpan { get; set; }
    }
}
0

:

ICriteria lvCriteria = NHibernateHelper.GetCurrentSession()
                                       .CreateCriteria(typeof(FunctionCall))
                                       .SetMaxResults(100)
                                       .AddOrder(Order.Desc("LogId"));

if (pvMsg.HasValue)
{                
       lvCriteria.Add(Restrictions.IsNotNull("Msg"))
                 .Add(Restrictions.Not(Restrictions.Eq("Msg", ""));
}

.

0

Source: https://habr.com/ru/post/1729639/


All Articles