C# 无法翻译LINQ表达式,将对其求值

C# 无法翻译LINQ表达式,将对其求值,c#,.net-core,entity-framework-core,.net-core-3.0,C#,.net Core,Entity Framework Core,.net Core 3.0,我的代码: public static IQueryable<ServicesData> Query(DbContext dbc, string path1 = null, string path2 = null, string path3 = null) => dbc.ServicesData.Where(sd => SQLmatch(sd.Path1, path1) && SQLmatch(sd.Path2, path2) &&

我的代码:

public static IQueryable<ServicesData> Query(DbContext dbc, string path1 = null, string path2 = null, string path3 = null) =>
    dbc.ServicesData.Where(sd => SQLmatch(sd.Path1, path1) && SQLmatch(sd.Path2, path2) && SQLmatch(sd.Path3, path3));

static bool SQLmatch(string match, string pattern) {
    if (match is null) match = "";
    if (pattern is null) pattern = "";
    bool wildcard = pattern.Contains("%") || pattern.Contains("_");
    return wildcard ? EF.Functions.Like(match, pattern) : object.Equals(match, pattern);
}

using (var dbc = new DbContext()) {
    var q = Query(dbc, "User", "%").ToList();
publicstaticiqueryable查询(DbContext-dbc,stringpath1=null,stringpath2=null,stringpath3=null)=>
dbc.ServicesData.Where(sd=>SQLmatch(sd.Path1,Path1)和&SQLmatch(sd.Path2,Path2)和&SQLmatch(sd.Path3,Path3));
静态boolsqlmatch(字符串匹配,字符串模式){
如果(匹配为空)匹配“”;
如果(模式为空)模式=”;
布尔通配符=pattern.Contains(“%”)|| pattern.Contains(“|”);
返回通配符?EF.Functions.Like(match,pattern):object.Equals(match,pattern);
}
使用(var dbc=new DbContext()){
var q=Query(dbc,“User”,“%”)。ToList();
使用最新的EFcore 3.0,此操作失败:

为警告生成错误 'Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning: LINQ表达式'where((SQLmatch([sd].Path1,_Path1_0)和also SQLmatch([sd].Path2,[UUUUPath2\U1])和SQLmatch([sd].Path3, __path3_2))“无法转换,将在本地计算。”。可以通过传递事件ID来抑制或记录此异常 将“RelationalEventId.QueryClientEvaluationWarning”添加到 “DbContext.OnConfiguring”中的“ConfigureWarnings”方法,或 “AddDbContext”

我将非常感谢您的帮助

编辑

问题似乎在于函数调用,如果我将SQLmatch中的代码放在一个表达式中,如下所示:

public static IQueryable<ServicesData> Query(DbContext dbc, string path1 = null, string path2 = null, string path3 = null) =>
    dbc.ServicesData.Where(sd => 
    ((path1 ?? "").Contains("%") || (path1 ?? "").Contains("_") ? EF.Functions.Like((sd.Path1 ?? ""), (path1 ?? "")) : object.Equals((sd.Path1 ?? ""), (path1 ?? "")))
    && ((path2 ?? "").Contains("%") || (path2 ?? "").Contains("_") ? EF.Functions.Like((sd.Path2 ?? ""), (path2 ?? "")) : object.Equals((sd.Path2 ?? ""), (path2 ?? "")))
    && ((path3 ?? "").Contains("%") || (path3 ?? "").Contains("_") ? EF.Functions.Like((sd.Path3 ?? ""), (path3 ?? "")) : object.Equals((sd.Path3 ?? ""), (path3 ?? ""))));
publicstaticiqueryable查询(DbContext-dbc,stringpath1=null,stringpath2=null,stringpath3=null)=>
dbc.ServicesData.Where(sd=>
((path1??).Contains(“%”| |(path1??)).Contains(“|”)EF.Functions.Like((sd.path1??),(path1??):object.Equals((sd.path1??),(path1??))
&&((路径2??).Contains(“%”)| |(路径2??)。Contains(“)?EF.函数。如((sd.path2??),(path2??):object.Equals((sd.path2??),(path2??))
&&((路径3??).Contains(“%”| |(路径3??)).Contains(“|”)EF.Functions.Like((sd.path3??),(path3??)):object.Equals((sd.path3??),(path3??));
它起作用了


为什么EFCore不能在单独的函数中处理代码?

在这种情况下,查询将转换为SQL并在数据库服务器中运行,并且您只能在这些类型的查询中使用LINQ支持的方法,这些方法可以直接转换为DB函数。因此,您不能在这些查询中使用自定义函数,即使没有t LINQ to实体支持所有内置的.net方法。有关支持的函数,请参阅。

谢谢!你能解释一下为什么在没有函数调用的情况下嵌入代码时它会工作吗?请参阅我的编辑-如果我没有在单独的函数中使用相同的代码,那么它也会工作是的,这就是我试图解释的,只有当代码直接在查询中时,它才会工作。它不会在单独的函数中工作,因为LINQ无法转换你的自定义函数函数到SQL。