如何替换Expression <func <t,bool>&gt;到linq到sql表达式的where子句</func <t,bool>

时间:2011-04-28 00:28:39

标签: c# .net linq linq-to-sql

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

Expression<Func<Purchase, bool>> condition = p => p.Price > 100;

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

但是,编译器无法确定使用哪个位置:IQuaryable&lt;&gt;或IEnumerable&lt;&gt;。 如果不将linq表达式转换为方法链,如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你不能那样做where condition。您可以将条件合并到where子句(其中purchase.Price&gt; 100)或使用 Where(条件)方法调用查询表达式,例如

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

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

答案 1 :(得分:0)

尝试:

where condition.Compile()(purchase);
相关问题