At point (3) in my code, I defined a query called query1, in which I defined a.Where lambda expression. This request is somehow dynamic, but still contains static elements; it always refers to the Type Employee and its (int) property ClientID.
Now I really like to refer to the type and its dynamic property based on the method parameters, which are shown below the point (1) as an example.
What I tried to do so far makes the static part of the query defined in (3) completely dynamic, replacing it with a more complex expression tree, as written in (4), (5) and (6). But when I try to add everything together, she says I'm calling. Where with the wrong parameters. I do not know how to call. Use the right options to create a fully dynamic selection.
Does anyone know how to solve this problem? I spent a day and did not find a solution.
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;
Thanks for reading my question! If you have questions about my question, ask them :)
c # lambda expression-trees where iqueryable
Wouter vegter
source share