我正在尝试用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);
这该怎么做?
EF.Functions
通常不应该被评估。
但要回答你的具体问题,你需要添加Microsoft.EntityFrameworkCore
程序集的引用和using
名称空间的导入(Microsoft.EntityFrameworkCore
),例如:
ScriptOptions options = ScriptOptions.Default
.AddReferences(references: typeof(T).Assembly)
.AddReferences(typeof(EF).Assembly) // <--
.AddImports(typeof(EF).Namespace); // <--