为什么EF内核不能判断这个值
{log => (True AndAlso Invoke(log => log.Message.StartsWith("test"), log))}
and It gives me this error :
"The LINQ expression 'log' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'."
While this expression runs normally :
{log => (True AndAlso Invoke(log => log.Message.StartsWith(value(Project.Logging.Specs+<> c__DisplayClass1_1).message), log))}
当我使用And Also扩展方法将2表达式加在一起时会发生这种情况
Expression<Func<Logs, bool>> exp1 = log => true;
Expression<Func<Logs, bool>> exp2 = log => log.Message.Contains("message");
exp1 = exp1.AndAlso(exp);
public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
var invokedExpr = Expression.Invoke(right, left.Parameters);
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body, invokedExpr), left.Parameters);
}
最后,我调用了dbContext
_context
.Logs
.Where(exp1);