如何评估Func 使用EF.Functions?

问题描述 投票:1回答:1

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

创建Func方法:

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个回答
2
投票

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

但要回答你的具体问题,你需要添加Microsoft.EntityFrameworkCore程序集的引用和using名称空间的导入(Microsoft.EntityFrameworkCore),例如:

ScriptOptions options = ScriptOptions.Default
    .AddReferences(references: typeof(T).Assembly)
    .AddReferences(typeof(EF).Assembly) // <--
    .AddImports(typeof(EF).Namespace); // <--
© www.soinside.com 2019 - 2024. All rights reserved.