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

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

How do I create an expression tree to represent ‘String.Contains(“term”)’ in C#?

Something like: class Foo { public string Bar { get; set; } } static void Main() { var lambda = GetExpression<Foo>(“Bar”, “abc”); Foo foo = new Foo { Bar = “aabca” }; bool test = lambda.Compile()(foo); } static Expression<Func<T, bool>> GetExpression<T>(string propertyName, string propertyValue) { var parameterExp = Expression.Parameter(typeof(T), “type”); var propertyExp = Expression.Property(parameterExp, propertyName); … Read more

Replace parameter in lambda expression

I would do it this way: Write a parameter-replacer expression-visitor that manipulates the original expression as follows: Gets rid of the parameter you don’t want entirely from the lambda signature. Replaces all uses of the parameter with the desired indexer expression. Here’s a quick and dirty sample I whipped up based on my earlier answer … 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

How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

We did something similar (not 100% the same, but similar) in a LINQ to SQL project. Here’s the code: public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) { var type = typeof(T); var property = type.GetProperty(ordering); var parameter = Expression.Parameter(type, “p”); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); MethodCallExpression … Read more

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

tech