我正在查看一些我为nhibernate编写的代码,其中我将基于一些条件应用于where子句。我有这样的事情:
dist
这里没有问题,我可以说:var query = session.QueryOver<Client>().Where(x => x.IsDeleted == false);
if (status == "active")
query.Where(x => x.IsActive == true);
然而,当我将其更改为:
query.where(xxx)
IQueryOver query = session.QueryOver<Client>().Where(x => x.IsDeleted == false);
if (status == "active")
//squiggly red lines on the line below!!!
query.Where(x => x.IsActive == true);
下的query.where(xxxx)
获得了红色波浪线。我检查了if
的返回类型,它返回session.QueryOver
类型,为什么抱怨?
是不是因为我试图创建接口类型的查询对象,而它应该是实现接口的具体类?
答案 0 :(得分:2)
这是您使用QueryOver
处理动态查询的方式:
var query = session.QueryOver<Client>()
.Where(x => x.IsDeleted == false);
if (status == "active")
query = query.Where(x => x.IsActive == true);
var clients = query.List(); // To execute the query and get the result (root entity)