How do I dynamically create an Expression predicate?

Original Like so: var param = Expression.Parameter(typeof(string), “p”); var len = Expression.PropertyOrField(param, “Length”); var body = Expression.Equal( len, Expression.Constant(5)); var lambda = Expression.Lambda<Func<string, bool>>( body, param); Updated re (p.Length== 5) && (p.SomeOtherProperty == “hello”): var param = Expression.Parameter(typeof(SomeType), “p”); var body = Expression.AndAlso( Expression.Equal( Expression.PropertyOrField(param, “Length”), Expression.Constant(5) ), Expression.Equal( Expression.PropertyOrField(param, “SomeOtherProperty”), Expression.Constant(“hello”) )); var lambda … Read more

How does PredicateBuilder work

Let’s say you have: Expression<Func<Person, bool>> isAdult = p1 => p1.Age >= 18; // I’ve given the parameter a different name to allow you to differentiate. Expression<Func<Person, bool>> isMale = p2 => p2.Gender == “Male”; And then combine them with PredicateBuilder var isAdultMale = isAdult.And(isMale); What PredicateBuilder produces is an expression that looks like this: … Read more

Access the value of a member expression

You can compile and invoke a lambda expression whose body is the member access: private object GetValue(MemberExpression member) { var objectMember = Expression.Convert(member, typeof(object)); var getterLambda = Expression.Lambda<Func<object>>(objectMember); var getter = getterLambda.Compile(); return getter(); } Local evaluation is a common technique when parsing expression trees. LINQ to SQL does this exact thing in quite a … 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 set value a property selector Expression

This works: The following helper method converts a getter expression into a setter delegate. If you want to return an Expression<Action<T,TProperty>> instead of an Action<T,TProperty>, just don’t call the Compile() method at the end. Note: The code is from Ian Mercer’s blog: http://blog.abodit.com/2011/09/convert-a-property-getter-to-a-setter/ /// <summary> /// Convert a lambda expression for a getter into a … Read more

C# LINQ to SQL: Refactoring this Generic GetByID method

What you need is to build an expression tree that LINQ to SQL can understand. Assuming your “id” property is always named “id”: public virtual T GetById<T>(short id) { var itemParameter = Expression.Parameter(typeof(T), “item”); var whereExpression = Expression.Lambda<Func<T, bool>> ( Expression.Equal( Expression.Property( itemParameter, “id” ), Expression.Constant(id) ), new[] { itemParameter } ); var table = DB.GetTable<T>(); return … Read more

How to Combine two lambdas [duplicate]

To complete Eric’s answer, using the new ExpressionVisitor introduced in .NET 4 rather than a custom rewriter: internal class ParameterReplacer : ExpressionVisitor { private readonly ParameterExpression _parameter; protected override Expression VisitParameter(ParameterExpression node) { return base.VisitParameter(_parameter); } internal ParameterReplacer(ParameterExpression parameter) { _parameter = parameter; } } class Program { static void Main(string[] args) { Expression<Func<string, bool>> … Read more