带有自定义方法的LINQ to Entities Where子句 [英] LINQ to Entities Where clause with custom method

查看:41
本文介绍了带有自定义方法的LINQ to Entities Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework Core(2.0),并且存在以下疑问.

I'm using Entity Framework Core (2.0) and I have the following doubt.

我不确定执行此操作时会发生什么:

I'm not sure about what happens when I do this:

context.Customers.Where(c => MyCustomMethod(c));

bool MyCustomMethod(Customer c) 
{
    return c.Name.StartsWith("Something");
}

它可以毫无问题地转换为SQL吗?

Does it translate to SQL without problems?

与写作不同吗?

context.Customers.Where(c => c.StartsWith("Something"));

简而言之,我可以将对Where句式的验证包装在方法中吗?会破坏对SQL的翻译吗?

In short, will I be able to wrap my validations for the Where clase inside a method? Does it break the translation to SQL?

推荐答案

否,您无法在EF LINQ查询中调用自定义方法,因为EF无法生成该方法的表达式树,因此无法将其转换为SQL

No, you cannot call your custom method in EF LINQ query because EF will not be able to generate expression tree of the method and so it cannot translate it to SQL.

有关表达式树的更多信息,请参考

For more info about expression trees, refer to the link.

这篇关于带有自定义方法的LINQ to Entities Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆