下面的C#代码:差异生成的SQL
Func<Customer, bool> predicate1 = s => s.Name == "Roger";
dbContext.Customers.Where(predicate1);
生成此SQL查询:
select col1,col2 from customers
注意在上面的SQL查询,没有where
子句。
然而,在此C#代码:
dbContext.Customers.Where(s => s.Name == "Roger");
它产生:
select col1,col2 from customers where name = 'Rogers'
为什么不一样?是否有一种方法来传递如上所述的谓词,并仍然使用where
子句生成sql查询?
第一个例子不会产生任何SQL,因为你用来代替表达Func键。 Func无法解析,并且在加载所有客户后,该过滤器将在服务器端工作 –