无法解析 Enumerable 和 Queryable 候选人之间的方法

Cannot resolve method between Enumerable and Queryable candidates

我在一个名为 Invoice 的 class 中有这个方法:

public static Expression<Func<Invoice, bool>> IsAllocated()
{
    return i => i.TotalAmountDue == i.GetAllocationsTotal();
}

我有一个这样的列表:

IQueryable<Invoice> invoices

我需要像那样过滤它(它是 Linq to Entity):

var filteredInvoices = invoices.Where(i => Invoice.IsAllocated());

在这一行中我遇到了两个错误:

Cannot resolve method ... candidates are .... one in Enumerable and the other on in Queryable.

还有:

Cannot convert expression type Expression<Func<Invoice,bool>> to return type 'bool'

我已经尝试了很多我在 SO 中发现的东西,但都没有成功。有人可以告诉我这里缺少什么,或者至少,这两个错误中的哪一个是问题的根源?

你的方法 returns 已经是一个合适的表达式树 - 你只需要调用它,而不是在 lambda 表达式中调用它 :

var filteredInvoices = invoices.Where(Invoice.IsAllocated());

表达式是表示而不是自己委托。您应该首先从中创建一个委托

static Expression<Func<Invoice, bool>> IsAllocatedExpr()
{
    return i => i.TotalAmountDue == i.GetAllocationsTotal();
}

public static Func<Invoice, bool> IsAllocated = IsAllocatedExpr().Compile();

然后是

var filteredInvoices = invoices.Where(i => Invoice.IsAllocated(i));