In C#, how do you make a dynamic lambda-based Linq expression from a string?

c# dynamic-linq expression-trees lambda linq


I'm having some difficulty creating Lambda-based Linq expressions from a string. Here is my basic case using this sample object/class:

public class MockClass
    public string CreateBy { get; set; }

Basically I need to convert a string like this:

string stringToConvert = “x => x.CreateBy.Equals(filter.Value, StringComparison.OrdinalIgnoreCase”;

Into a to predicate/linq expression:

System.Linq.Expressions.Expression<Func<T, bool>>  or in this example 
System.Linq.Expressions.Expression<Func<MockClass, bool>>

So it is equivalent to the Linq expression inside of the Where method below:

query = query.Where(x => x.CreateBy.Equals(filter.Value,

I've tried using the following helpers but can't seem to figure out how to get them to work in this type of case where I want to be able to build a linq expression from string that is not know ahead of time: (it’s now available as a NuGet package as well called “DynamicQuery”)

4/11/2012 10:27:54 PM

Accepted Answer

A similar question was asked here:

Is there an easy way to parse a (lambda expression) string into an Action delegate?

As I understand it, this 'Dynamic Query' is actually a framework for passing in restrictions for a Where clause without using a lambda expression.

The significance of that is that lambda expressions are not dynamic methods, they're anonymous methods. If you ever take a look in an assembly, you'll see that your lambda expressions are converted into closures with any free variables as fields. The class has a method with a signature matching yours, field variables are assigned at the point of invocation.

One good way to think about that is that it implies that your lambda expression is interpreted by the c# compiler at compile-time, and variables are resolved by instantiating an object from this class at run-time.

To demonstrate this, consider the following:

var myLambda = x => x * x

You'll notice this doesn't work. That's because, in order to create the related class/method, the compiler must know, at compile-time, the type of x.

All of this is important because the notion of a lambda expression doesn't exist at the CLR at run-time (in the same form it is in code). A string that looks like a lambda expression is exactly that...

5/23/2017 12:11:27 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow