How to create LINQ Expression Tree to select an anonymous type

This can be done, as mentioned, with the help of Reflection Emit and a helper class I’ve included below. The code below is a work in progress, so take it for what it’s worth… ‘it works on my box’. The SelectDynamic method class should be tossed in a static extension method class. As expected, you … Read more

Why can’t an expression tree contain a named argument specification?

Consider the following: static int M() { Console.Write(“M”); return 1; } static int N() { Console.Write(“N”); return 2; } static int Q(int m, int n) { return m + n; } … Func<int> f = ()=>Q(n : N(), m: M()); Expression<Func<int>> x = ()=>Q(n : N(), m: M()); Func<int> fx = x.Compile(); Console.WriteLine(f()); Console.WriteLine(fx()); You … Read more

C# 4 “dynamic” in expression trees

You can create an expression tree that represents a dynamic C# addition expression by passing the CallSiteBinder for a dynamic C# addition expression into Expression.Dynamic. You can discover the code to create the Binder by running Reflector on the original dynamic expression. Your example would go something like this: var x = Expression.Parameter(typeof(object), “x”); var … Read more

variable ” of type ” referenced from scope ”, but it is not defined

As indicated in the other answer, you have two expressions where both have a parameter named y. Those don’t automatically relate to each other. To properly compile your expression, you need to specify both source expression’s parameters: Expression<Func<string, bool>> e1 = (y => y.Length > 0); Expression<Func<string, bool>> e2 = (y => y.Length < 5); … Read more

How do I dynamically create an Expression predicate from Expression?

It’s hard to mix compiler-generated expression trees and hand-made ones, precisely because of this sort of thing – extracting out the ParameterExpressions is tricky. So let’s start from scratch: ParameterExpression argParam = Expression.Parameter(typeof(Service), “s”); Expression nameProperty = Expression.Property(argParam, “Name”); Expression namespaceProperty = Expression.Property(argParam, “Namespace”); var val1 = Expression.Constant(“Modules”); var val2 = Expression.Constant(“Namespace”); Expression e1 = … Read more

Mutating the expression tree of a predicate to target another type

It seems you’re generating the parameter expression twice, in VisitMember() here: var converted = Expression.MakeMemberAccess( base.Visit(node.Expression), activeRecordType.GetProperty(node.Member.Name)); …since base.Visit() will end up in VisitParameter I imagine, and in GetMany() itself: var lambda = Expression.Lambda<Func<ActiveRecord.Widget, bool>>( visitor.Visit(predicate.Body), predicate.Parameters.Select(p => visitor.Visit(p)); If you’re using a ParameterExpression in the body, it has to be the same instance (not … Read more

Construct LambdaExpression for nested property from string

Do you mean: static LambdaExpression CreateExpression(Type type, string propertyName) { var param = Expression.Parameter(type, “x”); Expression body = param; foreach (var member in propertyName.Split(‘.’)) { body = Expression.PropertyOrField(body, member); } return Expression.Lambda(body, param); } For example: class Foo { public Bar myBar { get; set; } } class Bar { public string name { get; … Read more

Expression.Lambda and query generation at runtime, simplest “Where” example

In the following query var result = query.Where(item => item.Name == “Soap”) the lambda expression is item => item.Name == “Soap” You only need to construct this part, not the Where call which accepts an expression tree. The expression tree for the lambda expression looks like this: Lambda / \ Equal Parameter / \ item … Read more