我有两个表达式,我想将它们链接起来,以便结果表达式包含两个输入表达式。
Expression<Func<IQueryable<Material>, object>> expression1 = x => x.Include(m => m.MaterialGroup);
Expression<Func<IQueryable<Material>, object>> expression2 = x => x.Include(m => m.MaterialSomething);
var expression3 = expression1.Update(expression2.Body, expression2.Parameters);
现在expression3
仅包含x => x.Include(m => m.MaterialSomething)
,因此它将覆盖第二个表达式。我希望它是x => x.Include(m => m.MaterialGroup).Include(m => m.MaterialSomething)
。
我打算实现的目的是以编程方式联接多个include表达式,以便能够构建更有效的系统,以期在EF Core中快速加载。
编辑: 这不是ANDing,ORing等问题的原因,因为我希望将这些表达式链接起来(像点链接一样),而不是逻辑上相连。
丹尼尔
答案 0 :(得分:0)
因为Include
是您的表达式的扩展方法
x => x.Include(m => m.MaterialGroup);
实际上是
x => QueryableExtensions.Include(x, m => m.MaterialGroup);
因此要链接您的表达式,您需要将Include
的第一个参数替换为对另一个Include
的调用
x => QueryableExtensions.Include(
QueryableExtensions.Include(x, m => m.MaterialSomething),
m => m.MaterialGroup);
下一个代码将执行此链接
public static Expression<Func<IQueryable<T>, object>> Chain<T>(
params Expression<Func<IQueryable<T>, object>>[] expressions)
{
if (expressions.Length == 0)
throw new ArgumentException("Nothing to chain");
if (expressions.Length == 1)
return expressions[0];
Expression body = expressions[0].Body;
var parameter = expressions[0].Parameters[0];
foreach (var expression in expressions.Skip(1))
{
var methodCall = (MethodCallExpression)expression.Body;
var lambda = (UnaryExpression)methodCall.Arguments[1];
body = Expression.Call(typeof(QueryableExtensions),
"Include",
new []{ typeof(T), ((LambdaExpression)lambda.Operand).Body.Type},
body, lambda
);
}
return Expression.Lambda<Func<IQueryable<T>, object>>(body, parameter);
}
用法:
var expression = Chain(expression1, expression2 /*, expression3 .... */);
您可以在线here进行测试
请注意,为简洁起见,此代码跳过了表达式验证。
答案 1 :(得分:0)
我想添加另一种方法来存档链lambda表达式:
在易于访问的位置添加以下静态方法
public static Expression<Func<T, bool>> ConcatLambdaExpression<T>(Expression<Func<T, bool>> firstExpression, Expression<Func<T, bool>> secondExpression)
{
var invokedThird = Expression.Invoke(secondExpression, firstExpression.Parameters.Cast<Expression>());
var finalExpression = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(firstExpression.Body, invokedThird), firstExpression.Parameters);
return finalExpression;
}
然后您可以通过以下方式使用它:
public PersonDTO GetAll()
{
Expression<Func<Person, bool>> expression = x => x != null;
expression = x => x.Name == "John";
Expression<Func<Person, bool>> pred = x => x.LastName == "Doe" || x.LastName == "Wick";
//result of expression would be:
////expression = x.Name == "John" && (x => x.LastName == "Doe" || x.LastName == "Wick")
expression = Utilities.ConcatLambdaExpression(expression, pred);
var result = Context.PersonEntity.Where(expression);
//your code mapping results to PersonDTO
///resultMap...
return resultMap;
}