简体   繁体   English

无法解析可枚举和可查询候选之间的方法

[英]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.

相关问题 “无法解决方法-候选人是”-编译错误 - “Cannot resolve method - candidates are” - compilation error 方法 Enumerable.Where 中的两个候选者 - Two candidates in method Enumerable.Where Skip()和Take()作为可枚举与可查询 - Skip() and Take() as Enumerable vs as Queryable 无法将可查询方法转换为可枚举方法。 这可能是实体框架中的一个问题 - Unable to convert a queryable method to an enumerable method. This is likely an issue in Entity Framework 用于Enumerable和Queryable扩展方法的Lambda表达式参数 - Lambda expression arguments for Enumerable and Queryable extension methods Dynamic LINQ,Select函数适用于Enumerable,但不适用于Queryable - Dynamic LINQ, Select function, works on Enumerable, but not Queryable 如何解决 Enumerable 和 MoreLINQ 之间不明确的 ZIP 调用? - How to resolve ambiguous ZIP call between Enumerable and MoreLINQ? 无法从用法推断方法 Queryable.OrderBy 的类型参数 - The type arguments for method Queryable.OrderBy cannot be inferred from the usage 无法从用法中推断方法Queryable.Select的类型参数 - The type arguments for method Queryable.Select cannot be inferred from the usage IQueryable和Queryable之间的区别 - Difference between IQueryable and Queryable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM