我的问题源于复杂的可重用逻辑规范。
我有以下Expression
:
Expression<Func<User, bool>> userExp =
user => user.UserInRoles.Any(userInRole => userInRole.RoleId == SystemRoles.SysAdmin.Id);
我需要获得以下能力:
new CommonContext().Set<Estate>().Where(estate => userExp.WithParameter(estate.CreatorUser)).ToList();
那么我怎样才能通过Creator
的Estate
实体到接受 User
的表达式中实体并最终使用 linq to sql
中的最终表达式?
问题是:WithParameter
编辑:
这个可以工作,但效率不高:
new CommonContext().Set<Estate>().ToList().Where(estate => userExp.Compile()(estate.CreatorUser)).ToList()
以下不是答案,因为 Invoke method
无法翻译为存储表达式:
Expression<Func<User, bool>> userExp =
user => user.UserInRoles.Any(userInRole => userInRole.RoleId == SystemRoles.SysAdmin.Id);
Expression<Func<Estate, User>> propAccessor = estate => estate.ApprovedByUser;
var estateParam = Expression.Parameter(typeof(Estate));
var userParam = Expression.Invoke(propAccessor, estateParam);
var translatedExp = Expression.Invoke(userExp, userParam);
var result = (Expression<Func<Estate, bool>>)Expression.Lambda(translatedExp, estateParam);
var exceptionProvider = new CommonContext().Set<Estate>().Where(result).ToList();
但我需要一些可以翻译成 Store Expression
的东西
也许最终的解决方案是分解然后重新组合表达式,如果是这样,我该如何封装它以便在类似情况下重用它? (因为这就是我想做的)
最佳答案
您将需要重写表达式。我为WithParameter做了一个扩展方法。
首先,我们需要从这个答案 https://stackoverflow.com/a/5431309/1798889 借用 ParameterVisitor 类并稍微调整一下。我们不想只是将一个表达式中的参数替换为另一个参数,我们希望删除该参数并将其替换为新的表达式。
public class ParameterVisitor : ExpressionVisitor
{
private readonly ReadOnlyCollection<ParameterExpression> from;
// Changed from ReadOnlyCollection of ParameterExpression to Expression
private readonly Expression[] to;
public ParameterVisitor(ReadOnlyCollection<ParameterExpression> from, params Expression[] to)
{
if (from == null) throw new ArgumentNullException("from");
if (to == null) throw new ArgumentNullException("to");
if (from.Count != to.Length)
throw new InvalidOperationException(
"Parameter lengths must match");
this.from = from;
this.to = to;
}
protected override Expression VisitParameter(ParameterExpression node)
{
for (int i = 0; i < from.Count; i++)
{
if (node == from[i]) return to[i];
}
return node;
}
}
请注意,我将 To 参数更改为表达式而不是 ParameterExpression。
现在我们要创建 WithParameter
public static class QueryableExtensions
{
public static Expression<Func<TResult, bool>> WithParameter<TResult, TSource>(this Expression<Func<TSource, bool>> source, Expression<Func<TResult, TSource>> selector)
{
// Replace parameter with body of selector
var replaceParameter = new ParameterVisitor(source.Parameters, selector.Body);
// This will be the new body of the expression
var newExpressionBody = replaceParameter.Visit(source.Body);
return Expression.Lambda<Func<TResult, bool>>(newExpressionBody, selector.Parameters);
}
}
在此,我们采用选择器来了解我们想要什么属性,并将 Expression> 的参数替换为该属性。
你使用它就像
new CommonContext().Set<Estate>().Where(userExp.WithParameter<Estate, User>(estate => estate.CreatorUser)).ToList();
警告的话,我不知道 Linq to SQL,也没有针对它进行测试,也没有针对 IQueryable 进行测试,但它确实适用于 IEnumberable。我不明白为什么它不起作用,因为最后两个表达式的调试 View 看起来相同。
Expression<Func<Estate, bool>> estateExp = estate => estate.CreatorUser.UserInRoles.Any(userInRole => userInRole.RoleId == SystemRoles.SysAdmin.Id);
与
是相同的表达式树userExp.WithParameter(estate => estate.CreatorUser)
关于c# - 将一个表达式作为参数传递到另一个表达式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26400831/