Fluent NHibernate Where on Empty String
本文关键字:Empty String on Where NHibernate Fluent | 更新日期: 2023-09-27 17:49:20
我对FluentNH中的IQueryOver<T,T>
应用.Where()
-限制,如下:
.Where(x => x.Col1 == null || x.Col1 == "");
生成以下SQL:
WHERE (Col1 IS NULL OR Col1 = NULL)
我怎样才能让NH明白空字符串就是空字符串?
你可以这样写你的Where
子句:
.Where(Restrictions.On<ClassType>(obj => obj.Col1).IsNull ||
Restrictions.On<ClassType>(obj => obj.Col1).IsLike(@""))
或者,如果您在多个查询中执行此操作,则应该考虑创建查询扩展:
public static class QueryExtention {
public static IQueryOver<E, F> WhereStringIsNullOrEmpty<E, F>(this IQueryOver<E, F> query, Expression<Func<E, object>> expression) {
var property = Projections.Property(expression);
var criteria = Restrictions.Or(Restrictions.IsNull(property),
Restrictions.Eq(property, string.Empty));
return query.Where(criteria);
}
}
那么你应该能够创建如下内容:
.QueryOver<ClassType>()
.WhereStringIsNullOrEmpty(obj => obj.Col1)