[英]Cannot resolve method between Enumerable and Queryable candidates
I have this method in a class called Invoice: 我在名为Invoice的类中有此方法:
public static Expression<Func<Invoice, bool>> IsAllocated()
{
return i => i.TotalAmountDue == i.GetAllocationsTotal();
}
I have a list like this: 我有一个这样的清单:
IQueryable<Invoice> invoices
And I need to filter it like that (it's Linq to Entity): 我需要像这样过滤它(Linq到Entity):
var filteredInvoices = invoices.Where(i => Invoice.IsAllocated());
In this line I'm getting two errors: 在这一行中,我遇到两个错误:
Cannot resolve method ... candidates are .... one in Enumerable and the other on in Queryable. 无法解析方法...候选对象是....一个在Enumerable中,另一个在Queryable中。
And also: 并且:
Cannot convert expression type
Expression<Func<Invoice,bool>>
to return type 'bool' 无法将表达式类型Expression<Func<Invoice,bool>>
为返回类型'bool'
I've tried a lot of things I've found in SO with no luck. 我尝试了很多在SO中发现的事情,但是没有运气。 Can someone say me what is missing here or at least, which one of the two errors is at the root of the problem? 有人可以说出这里或至少缺少什么,这两个错误中的哪个是问题的根源?
您的方法已经返回了适当的表达式树-您只需要调用它,而不用在lambda表达式中调用它:
var filteredInvoices = invoices.Where(Invoice.IsAllocated());
Expression are representation and not delegate by themselves. 表达式是表示形式,而不是自己委托。 You should create a delegate out of it first 您应该首先从中创建一个委托
static Expression<Func<Invoice, bool>> IsAllocatedExpr()
{
return i => i.TotalAmountDue == i.GetAllocationsTotal();
}
public static Func<Invoice, bool> IsAllocated = IsAllocatedExpr().Compile();
and then 接着
var filteredInvoices = invoices.Where(i => Invoice.IsAllocated(i));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.