Search code examples
c#nhibernatecriterianhibernate-criteria

How to add a true/false to a disjunction/conjunction in nhibernate?


I saw a few older posts mentioning MyDisjunction.Add(Restrictions.Sql("(1=1)")), but I couldn't find the Sql function (does it still exist?).

Instead, I am using MyDisjunction.Add(Restriction.Where<MyObject>(x => x.SomeProperty == x.SomeProperty)) (!= for false), but this feels like I'm abusing the use of Restriction.Where. Is there something more natural to use?


Solution

  • Check the class Expression

    var alwaysTrue = Expression.Sql("1 = 1");
    
    ...
       .Add(alwaysTrue)
    

    But this is a code snippet from Expression class source:

    namespace NHibernate.Criterion
    {
        /// <summary>
        /// This class is semi-deprecated. Use <see cref="Restrictions"/>.
        /// </summary>
        /// <seealso cref="Restrictions"/>
        public sealed class Expression : Restrictions
        ...