我正在尝试在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); // <--