What is the best resource for learning C# expression trees in depth?

Chapter 11 (Inside Expression Trees) and chapter 12 (Extending Linq) of Programming Microsoft Linq (ISBN 13: 978-0-7356-2400-9 or ISBN 10: 0-7356-2400-3) has been invaluable for me. I haven’t read Jons book, but he is a quality guy and explains things well, so I assume that his coverage would also be good. Another great resource is … Read more

Internal .NET Framework Data Provider error 1025

While the other answers are true, note that when trying to use it after a select statement one has to call AsQueryable() explicitly, otherwise the compiler will assume that we are trying to use IEnumerable methods, which expect a Func and not Expression<Func>. This was probably the issue of the original poster, as otherwise the … Read more

Practical use of expression trees [closed]

As Jon notes, I use them to provide generic operators with .NET 3.5. I also use them (again in MiscUtil) to provide fast access to non-default constructors (you can’t use Delegate.CreateDelegate with constructors, but Expression works fine). Other uses of manually created expression trees: object cloning dynamic LINQ sorting as a compiler But really, Expression … 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 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

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

Compiled C# Lambda Expressions Performance

Could it be that the inner lambdas are not being compiled?!? Here’s a proof of concept: static void UsingCompiledExpressionWithMethodCall() { var where = typeof(Enumerable).GetMember(“Where”).First() as System.Reflection.MethodInfo; where = where.MakeGenericMethod(typeof(int)); var l = Expression.Parameter(typeof(IEnumerable<int>), “l”); var arg0 = Expression.Parameter(typeof(int), “i”); var lambda0 = Expression.Lambda<Func<int, bool>>( Expression.Equal(Expression.Modulo(arg0, Expression.Constant(2)), Expression.Constant(0)), arg0).Compile(); var c1 = Expression.Call(where, l, Expression.Constant(lambda0)); var … Read more