Viewing the IL code generated from a compiled expression
Yes! Use this tool: https://github.com/drewnoakes/il-visualizer This was incredibly useful when I was implementing and debugging Compile, as I’m sure you can imagine.
Yes! Use this tool: https://github.com/drewnoakes/il-visualizer This was incredibly useful when I was implementing and debugging Compile, as I’m sure you can imagine.
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
I had the same problem, but somewhat more complex, and Darin Dimitrov’s answer gave me a good start. I’ll post my results here, despite the fact that this is an “old” question . Case 1: The root object is an instance member this.textBox.Text // where ‘this’ has type ‘Form’ … is equivalent to the following … Read more
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
Have a look at Flee (Fast Lightweight Expression Evaluator) on CodePlex.
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
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
There are several things wrong with how you’re going about it. You’re mixing abstraction levels. The T parameter to GetAnyExpression<T> could be different to the type parameter used to instantiate propertyExp.Type. The T type parameter is one step closer in the abstraction stack to compile time – unless you’re calling GetAnyExpression<T> via reflection, it will … Read more
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
Using reflection and expression-trees you can provide the parameters and then call OrderBy function, Instead of returning Expression<Func<Task, T>> and then calling OrderBy. Note that OrderBy is an extension method and has implemented in both System.Linq.Enumarable and System.Linq.Queryable classes. The first one is for linq-to-objects and the latter is for linq-to-entities. entity-framework needs the expression … Read more