简体   繁体   English

使用AsExpandable()奇怪的Linq to Entities行为

[英]Strange Linq to Entities Behavior with AsExpandable()

Consider the following Person entity: 考虑以下Person实体:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Using the following Expression (constructed using PredicateBuilder ) as the criteria: 使用以下Expression (使用PredicateBuilder构建)作为标准:

var byName = PredicateBuilder.True<Person>().And(x => x.FirstName == "Chaim");

When invoked using the following syntax, the generated SQL is fine (includes the WHERE statement): 使用以下语法调用时,生成的SQL很好(包括WHERE语句):

ctx.Set<Person>().AsExpandable().Where(x => byName.Invoke(x));

However, when invoked using this slightly different syntax, no SQL WHERE is involved and the filtering is being done by Enumerable.Where instead: 但是,当使用这种稍微不同的语法调用时,不涉及SQL WHERE ,并且Enumerable.Where正在进行过滤:

ctx.Set<Person>().AsExpandable().Where(byName.Invoke);

Any thoughts? 有什么想法吗?

There is no implicit conversion from a method group to an Expression (of a corresponding delegate type). 没有从方法组到Expression (对应委托类型)的隐式转换。 There is an implicit conversion from a method group to a delegate of a matching signature. 从一个方法到组匹配的签名的一个代表的隐式转换。 Therefore only the IEnumerable overload matches. 因此只有IEnumerable重载匹配。

Of course, that's not to say that you need to use a lambda. 当然,这并不是说你需要使用lambda。 Just write: 写吧:

ctx.Set<Person>().AsExpandable().Where(ByName);

Since you're passing in an expression ( ByName is, after all, an Expression<Person, bool> already, which is exactly what Queryable.Where<Person> requires) this will evaluate as a query, not in linq to objects. 因为你传入一个表达式( ByNameByName是一个Expression<Person, bool> ,这正是Queryable.Where<Person>要求的),这将作为查询进行评估,而不是linq对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM