c# – 使用表达式树创建完全动态的where子句并在IQueryable上执行

栏目: C# · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/3444457/create-fully-dynamic-where-clause-with-expression-tree-and-execute-on-iqueryable

在我的代码中的第(3)点,我定义了一个名为query1的查询,其中我定义了一个.Where lambda表达式.此查询在某种程度上是动态的,但仍包含静态元素,它始终引用Type Employee及其(int)属性ClientID.

现在我非常希望根据方法参数来引用类型及其属性动态,例如,如下面的点(1)所示.

到目前为止,我尝试的是通过将更详细的表达式树替换为(4),(5)&中所写的更复杂的表达式树,使得在点(3)下定义的查询的静态部分完全动态化. (6).但是,当我尝试将所有内容添加到一起时,它说我打电话.有错误的参数.我不知道如何调用.使用正确的参数来创建一个完全动态的选择.

有人知道解决这个问题吗?我花了一天时间搜索,到目前为止还没有找到解决方案.

dsMain domainService = new dsMain();


        //(1)i want to rewrite the following four variables to method-parameters
        Type entityType = typeof(Employee);
        String targetProperty = "ClientID";
        Type entityProperty = typeof(Employee).GetProperty(targetProperty).PropertyType;
        int idToDelete = 5;


        //(2)create expression-function: idToDelete == entityType.targetProperty (in this case: Employee.ClientID)
        ParameterExpression numParam = Expression.Parameter(entityProperty, targetProperty.Substring(0, 3));
        ConstantExpression equalTarget = Expression.Constant(idToDelete, idToDelete.GetType());
        BinaryExpression intEqualsID = Expression.Equal(numParam, equalTarget);
        Expression<Func<int, bool>> lambda1 =
                    Expression.Lambda<Func<int, bool>>(
                    intEqualsID,
                    new ParameterExpression[] { numParam });

        //(3)I want to create query1 fully dynamic, so defining Employee or an other type and its property at run time
        WhereClause = lambda1.Compile();
        IQueryable<Employee> employees = domainService.GetEmployees();
        var query1 = employees.Where<Employee>(C => WhereClause.Invoke(C.ClientID)).Expression;



        //(4)create the operand body {value(ASP.test_aspx).WhereClause.Invoke(E.ClientID)}
        var operandbodyMethod = WhereClause.GetType().GetMethod("Invoke");
        var operandbodyType = typeof(System.Boolean);
        var operandbodyArgs1Expression = Expression.Parameter(entityType, entityType.Name.Substring(0, 1));
        var operandbodyArgs1 = Expression.MakeMemberAccess(operandbodyArgs1Expression, entityType.GetMember(targetProperty)[0]);
        var operandBodyObjectExp = Expression.Constant(this, this.GetType());
        var operandbodyObject = Expression.MakeMemberAccess(operandBodyObjectExp, this.GetType().GetMember("WhereClause")[0]);

        //(5)create the operand {E => value(ASP.test_aspx).WhereClause.Invoke(E.ClientID)}
        var operandbody = Expression.Call(operandbodyObject, operandbodyMethod, operandbodyArgs1);
        var operandParameter = Expression.Parameter(entityType, entityType.Name.Substring(0, 1));
        var operandType = typeof(Func<,>).MakeGenericType(entityType, typeof(System.Boolean));

        //(6)
        var operand = Expression.Lambda(operandType, operandbody, new ParameterExpression[] { operandParameter });
        var expressionType = typeof(Expression<>).MakeGenericType(operandType);
        var completeWhereExpression = Expression.MakeUnary(ExpressionType.Quote, operand, expressionType);


        //(7)the line below does not work
        var query2 = employees.Where<Employee>(completeWhereExpression).Expression;

非常感谢您阅读我的问题!

如果您对我的问题有疑问,请询问他们:)

这很难单独查看,但首先发生的事情是Compile看起来不适合IQueryable – 这很少会起作用(LINQ-to-Objects是例外).

WhereClause.Invoke(C.ClientID)的等价物是使用Expression.Invoke来调用子表达式,但即便如此:LINQ-to-SQL将支持它,EF(至少3.5)不支持(也许“没有”;我没有重新检查4.0).最终,将lambda1创建为Expression<Func<Employee,bool>>会更加健壮.如果可能的话:

ParameterExpression empParam = Expression.Parameter(typeof(Employee),"emp");
    ConstantExpression equalTarget = Expression.Constant(idToDelete, idToDelete.GetType());
    BinaryExpression intEqualsID = Expression.Equal(
        Expression.PropertyOrField(empParam, targetProperty), equalTarget);
    Expression<Func<Exmployee, bool>> lambda1 =
                Expression.Lambda<Func<int, bool>>(
                intEqualsID,
                empParam);

然后将其传递给Where:

var query1 = employees.Where(lambda1);

翻译自:https://stackoverflow.com/questions/3444457/create-fully-dynamic-where-clause-with-expression-tree-and-execute-on-iqueryable


以上所述就是小编给大家介绍的《c# – 使用表达式树创建完全动态的where子句并在IQueryable上执行》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具