如何将 Expression<Func<T,bool>> 替换为 linq to sql 表达式的 where 子句



我想将以下谓词设置为用表达式语法编写的linq语句的where子句。

Expression<Func<Purchase, bool>> condition = p => p.Price > 100;
from purchase in dc.GetTable<Purchase>()
where condition
select ...

但是,编译器无法确定使用哪个Where:IQuayable<>或IEnumerable<>。如何在不将linq表达式转换为方法链的情况下解决此问题?

不能这样做where condition。您可以将条件包含在where子句中(where purchase.Price>100(,也可以在查询表达式中使用where(condition(方法调用,如

from purchase in dc.GetTable<Purchase>().Where(condition)
select ...

这是你可以将它们组合在一起的方式。

尝试:

where condition.Compile()(purchase);

最新更新