0

我正在使用实体框架核心(2.0),我有以下疑问。LINQ to Entities Where子句与自定义方法

我不知道我这样做的时候会发生什么:

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

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

是否翻译没有问题到SQL?

它比写作不同:

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

总之,我就能换一个方法内我对那里化酶验证?它打破了对SQL的翻译吗?

+1

你试过了吗?结果是什么? – Flater

+3

这是行不通的,因为EF不知道如何处理你的'MyCustomMethod' – DavidG

+1

我觉得你可以在这里面调用这个内部过程,在这种情况下 –

回答

3

,如果你需要从方法的字符串,你可以这样写

from customer in contetx.Customer 
       let str = GetString() 
       where Name.Any(c=> c.StartsWith(str)) 
       select customer; 

string GetString() 
{ 
    return "Something"; 
} 

我硝基甲苯知道该查询是有帮助的,但是这可以实现