【问题标题】:How to Evaluate Func<T, bool> with use EF.Functions?如何使用 EF.Functions 评估 Func<T, bool>?
【发布时间】:2019-04-08 14:09:46
【问题描述】:

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

创建函数方法:

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);

如何做到这一点?

【问题讨论】:

    标签: c# entity-framework-core roslyn func csharpscript


    【解决方案1】:

    EF.Functions 通常不应该被评估。

    但要回答您的具体问题,您需要将 reference 添加到 Microsoft.EntityFrameworkCore assemblyimport (using) Microsoft.EntityFrameworkCore 命名空间,例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多