Nhibernate + QueryOver: filter with Where ignoring sensitive

后端 未结 5 1225
一生所求
一生所求 2021-01-11 22:54

I am trying to build a simple query in nHibernate with QueryOver but I want it to convert everything lower-case or ignore sensitive:

Domain.User User = Sessi         


        
5条回答
  •  -上瘾入骨i
    2021-01-11 23:23

    public static class QueryOverExtension
    {
        /// 
        /// This method is used in cases where the root type is required
        /// Example: .WhereEqualInsensitive(t => t.Property, stringValue)
        /// 
        public static IQueryOver WhereEqualInsensitive(this IQueryOver queryOver, Expression> path, string value)
        {
            return queryOver.Where(Restrictions.Eq(Projections.SqlFunction("upper", NHibernateUtil.String, Projections.Property(path)), value.ToUpper()));
        }
    
        /// 
        /// This method is used in cases where the root type is NOT required
        /// Example: .WhereEqualInsensitive(() => addressAlias.DefaultEmail, contactEmailAddress)
        /// 
        public static IQueryOver WhereEqualInsensitive(this IQueryOver queryOver, Expression> path, string value)
        {
            return queryOver.Where(Restrictions.Eq(Projections.SqlFunction("upper", NHibernateUtil.String, Projections.Property(path)), value.ToUpper()));
        }
    }
    

    Usages:

    Session.QueryOver()
               .WhereEqualInsensitive(t => t.Property, value)
    
    ChildDTO childAlias = null;
    Session.QueryOver()
               .JoinAlias(t => t.ChildDTO, () => childAlias)
               .WhereEqualInsensitive(() => myAlias.Property, value)
    

提交回复
热议问题