将Expression <Func <T,bool >>转换为Expression <Func <T1,bool >>,以便T是T1的成员

披头士乐队1692

我们有一个类型为type的实体,T1其成员为type T像这样的东西:

public class T1
{
    public T Member{get;set;}
}

用户可以使用我们的UI给我们提供T过滤器,并将其转换为获取T并返回bool的函数的表达式 (Expression<Func<T,bool>>)

我想知道是否可以将其转换为获取T1并返回bool的函数的表达式。

实际上,我想将其转换为:

(t=>t.Member1==someValue && t.Member2==someOtherValue);

对此:

(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);
格伦迪

您可以通过几种方法来做到这一点。

首先也是最简单的:使用Expression.Invoke

Expression<Func<T, bool>> exprT = t.Member1==someValue && t.Member2==someOtherValue
ParameterExpression p = Expression.Parameter(typeof(T1));
var expr = Expression.Invoke(expr, Expression.PropertyOrField(p, "Member"));
Expression<Func<T1, bool>> exprT1 = Expression.Lambda<Func<T1, bool>>(expr, p);

但在这种情况下,您不会

t1 => (t=>(t.Member1==someValue && t.Member2==someOtherValue))(t1.Member), 

代替

(t1=>t1.Member.Member1==someValue && t1.Member.Member2==someOtherValue);

对于替换,您可以使用ExpressionVisitor类,例如

    class V : ExpressionVisitor
    {
        public ParameterExpression Parameter { get; private set; }
        Expression m;
        public V(Type parameterType, string member)
        {
            Parameter = Expression.Parameter(parameterType);
            this.m = Expression.PropertyOrField(Parameter, member);
        }
        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (node.Type == m.Type)
            {
                return m;
            }
            return base.VisitParameter(node);
        }
    }

并使用它

var v = new V(typeof(T1), "Member");
var exprT1 = Expression.Lambda<Func<T1, bool>>(v.Visit(exprT.Body), v.Parameter);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法从 `Expression<Func<T1, T2>>` 转换为 `Expression<Func<object, object>>`

将表达式转换为Expression <Func <T,bool >>

如何将 BinaryExpression 转换为 Expression<Func<T, bool>>?

如何将Expression <Func <T,object >>转换为Expression <Func <T,bool >>?

C#将Func <T1,object>转换为Func <T1,T2>

将Func <T,TProperty>转换为Expression <Func <T,Property >>

C#Func <T,bool>与Expression <Func <T,bool >>

方法重载中的 Expression<Func<T,bool>> 与 Func<T,bool>

如何将委托`Func<T1, Func<T2, Task<TResult>>>` 转换为`Func<T1, Task<Func<T2, TResult>>`?

有没有办法将Func <T1,bool>映射到Func <T2,bool>?

将Expression <Action <T >>转换为Expression <Func <T >>

将Expression <Func <T,bool >>与bool进行比较

正文Expression <Func <T,bool >>的复杂编辑

将Expression <Func <T,V >>转换为Expression <Func <T,Nullable <V >>>

将Expression <Func <T,TProperty >>转换为Expression <Func <object,object >>,反之亦然

将Expression <Func <T,U >>转换为Expression <Func <object,object >>

将谓词<T>转换为Func <T,bool>

用类型强制类型转换Expression <Func <T,bool >>

将 func(T) 转换为 func(any)

如何在C#中将LambdaExpression转换为Expression <Func <T,bool >>

将Expression <Func <TEntity,IEnumerable <TProperty >>> valueSelector,TValue []值转换为Expression <Func <TElement,bool >>

从Expression <Func <T,bool >>实体框架返回所有实体

实体框架过滤器“ Expression <Func <T,bool >>”

将MemberInfo反射到Func <T1,T2>

为什么要使用Expression <Func <T >>而不是Func <T>?

无法理解如何编码Func <Func <T1,T2>,T3>

如何从 Expression<Func<TSource, TSourceMember>> 转换为 Func<TSource, bool> 条件

如何将Expression <Func <TEntity,bool >>映射到Expression <Func <TDbEntity,bool >>

如何将多个Expression <Func <T,bool >>组合为单个表达式以针对DbContext执行?