queryover and (x like 'a' or y like 'a')

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 17:57:58
query.Where(Restrictions.On<Type>(x => x.Code).IsLike(codePart) ||
            Restrictions.On<Type>(x => x.Description).IsLike(codePart))

You could use the NHibernate Disjunction class to do this in a more elegant (IMHO) fashion:

var disjunction= new Disjunction();

disjunction.Add(Restrictions.On<Type>(e => e.Code).IsLike(codePart));
disjunction.Add(Restrictions.On<Type>(e => e.Description).IsLike(codePart));
//(and so on)

and then:

query.Where(disjunction)

Each "OR" is a separate instruction, which helps if you want to add the predicates conditionally.

Another version of this, which depending on taste, you may like, is as follows:

query.Where(Restrictions.Disjunction()
         .Add(Restrictions.On<Type>(e => e.Code).IsLike(codePart))
         .Add(Restrictions.On<Type>(e => e.Description).IsLike(codePart)));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!