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

标签 c# lambda linq-to-entities

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

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 中发现的东西,但都没有成功。有人可以告诉我这里缺少什么,或者至少,这两个错误中的哪一个是问题的根源?

最佳答案

您的方法已经返回了适当的表达式树 - 您只需要调用它,而不是在 lambda 表达式中调用它:

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

关于c# - 无法解析 Enumerable 和 Queryable 候选人之间的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856378/

相关文章:

c# - 如何删除字符串中的\n?

c# - 从代码加载 WPF 中的图像

c# - 使用 subselect 和 groupby 的 LINQ 仅获取列表中每个项目的最新版本

c# - IEnumerable 与 IQueryable

c# - 方法 GetPrice() 无法转换为商店表达式

c# - 在 Xamarin Forms 背后的代码中设置 ListView ItemSource 绑定(bind)

c# - 在 MVC3 中的布局文件中显示帐户信息的最佳方式

c# - 需要帮助来逆转此 Lambda 操作的逻辑

c++ - std::visit 的 C++17 示例中令人困惑的模板

c# - 如何将 Entity Framework 5 中的 DateTime SELECT 转换为仅 SELECT 日期部分?