我想将 where 子句添加到 linq 语句,但它的行为并不像我预期的那样。 当我使用这段代码时:
IQueryable<Employee> EmpQuery = from e in Session.Query<Employee>() where e.Surname == "Test" select e;
EmpQuery.ToList();
或者我使用这段代码:
IQueryable<Employee> EmpQuery = (from e in Session.Query<Employee>() select e).Where(e => e.Surname == "Test");
EmpQuery.ToList();
where 子句包含在 SQL 命令中,但是当我这样尝试时:
IQueryable<Employee> EmpQuery = from e in Session.Query<Employee>() select e;
EmpQuery.Where(e => e.Surname == "Test");
SQL 命令中不包含 where 子句。为什么是这样?还有另一种方法可以动态地将条件添加到 Nhibernate Linq 查询吗?
最佳答案
您没有使用 Where
的返回值。 LINQ 是围绕功能概念设计的 - 调用 Where
不会修改现有查询,它会返回一个应用过滤器的新查询。现有查询保持原样 - 这意味着您可以将它重新用于(比如)不同的过滤器。
请注意,您当前的查询表达式(from x in y select x
,实际上)毫无意义。我建议简单地写:
var query = Session.Query<Employee>().Where(e => e.Surname == "Test");
关于linq - Nhibernate linq。 where扩展方法并没有在SQL命令中加入where子句,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4969566/