如何使用 EF 评估 Func<T、bool>。功能?



我正在尝试在CSharpScript.EvaluateAsync中使用EF.Functions.Like来评估func<T, bool>,但是调用方法时运行时出现错误(当前上下文中不存在名称"EF"GetPredicate()(。

创建函数方法:

public static class Helper
{
    public static async Task<Func<T, bool>> GetPredicate<T>(string stringExpression)
            {
                ScriptOptions options = ScriptOptions.Default.AddReferences(references: typeof(T).Assembly);
                Func<T, bool> discountFilterExpression = await CSharpScript.EvaluateAsync<Func<T, bool>>(stringExpression, options);
                return discountFilterExpression;
            }
}

调用方法:

string expString = "x => EF.Functions.Like(x.CodeId, pattern) || EF.Functions.Like(x.Name, pattern) || EF.Functions.Like(x.CategoryId, pattern)";
Func<MyClass, bool> exp = await Helper.GetPredicate<MyClass>(expString);

怎么做?

通常不应该评估EF.Functions

但是要回答您的具体问题,您需要添加对Microsoft.EntityFrameworkCore程序集引用Microsoft.EntityFrameworkCore命名空间导入(using(,例如

ScriptOptions options = ScriptOptions.Default
    .AddReferences(references: typeof(T).Assembly)
    .AddReferences(typeof(EF).Assembly) // <--
    .AddImports(typeof(EF).Namespace); // <--

最新更新