expression-trees

Convert Expression<Func<T, T2, bool>> to Expression<Func<T2, bool>> by introducing a constant for T

I have an expression in the format of Expression<Func<T, T2, bool>> that I need to convert into an expression on the format of Expression<Func<T2, bool>> by replacing the T in the first expression with a constant value. I need this to stay as an expression so I can't just Invoke the expression with a constant as the first parameter. I'...

What does ExpressionVisitor.Visit<T> Do?

Before someone shouts out the answer, please read the question through. What is the purpose of the method in .NET 4.0's ExpressionVisitor: public static ReadOnlyCollection<T> Visit<T>(ReadOnlyCollection<T> nodes, Func<T, T> elementVisitor) My first guess as to the purpose of this method was that it would visit each node in each tree ...

Adding event handler and expression trees.

How to make lambda like this Action<EventHandler<TEventArgs>> adder = vs => GenericEvent += vs using expression trees. This code doesn't work: Expression<Func<EventHandler<TEventArgs>>> handler = () => GenericEvent; var vs = Expression.Parameter(typeof(EventHandler<TEventArgs>), "vs"); var adder = Expression.Lambda<Action<EventHa...

How to get runtime-argument info from extression tree parameter array

Okay first to explain the rules: I need a function that constructs a delegate matching any delegate type that encapsulates a body of which invokes a delegate of type (Object) (Object[] args) with 'args' containing all of the arguments passed to the original delegate during invocation. My work so far: delegate void TestDelegate(int...

Linq expression replace parameter type

I have a predicate, that was made from lambda expression after all extension methods were served. For example: (new List<string>).Where(i => i.Contains("some")).Where(i => i.Contains("second_some")); "List<>" is only example, there could be my custom data context or object collection. So, I have an "Expression<...>", and it's base typ...

How to evaluate an expression in prefix notation

I am trying to evaluate a list that represents an expression in prefix notation. Here is an example of such a list: [+, [sin, 3], [- 10 5]] What is the best way to evaluate the value of the list ...

Convert expression tree linear representation to postfix

Let's say I have an expression tree encoded as a linear (array) representaion. The tree is constructed from the array by "filling" the tree in level-order, left-to-right and top-to-bottom. For example, the linear representation: +*243 would parse into the following tree: + / \ / \ * 2 / \ / ...

Adding Conditionals & Functions to a Math Parser

I have a binary tree based mathematical expression parser I built, which works great for 'normal' math, like: (3.5 * 2) ^ 1 / (1 << 6). however, I would like to expand it a little to add a ternary selection operator, mirroring the one from C: {expr} ? {true-expr} : {false-expr}. I would also like to add functions, like sin(x) or ave(...)...

Using expression tree to navigate and return an object that owns a property

Hi, Given an expression that points to a property in an object graph, I want to retrieve the instance that owns the property withing that graph. I've been struggling. The current code just gives me System.InvalidOperationException : Lambda Parameter not in scope [Test] public void TestExpression() { var person = n...

C# 4.0: Expression trees vs. CodeDom

What are the differences between Expression trees and CodeDom? Which should I use for which scenario? ...

How to create an Expression Tree to do something similar to the SQL "Like " command

I’m working on some expression tree code written by a colleague and am looking into the possibility of adding additional expressions. It currently supports: equals, not-equals, IsNull etc. I need to add something that will allow it to use a wildcard comparison similar to the SQL “Like” command or using regular expressions. At the moment ...

Creating an expression tree that calls a method

Is it possible to create an expression tree that directly calls a method? For example, consider the following method: public static int MyFunc(int a, int b) { return a + b; } I would like to create an expression tree that calls MyFunc with parameters a=1 and b=2. One way to accomplish this is with reflection: var c1 = Expression....

How does one create a .NET Expression with NodeType of ExpressionType.Index?

I'm writing code that evaluates .NET Expression trees. I'm trying to create a C# 4 test to exercise my handling of an ExpressionType.Index, but I can't figure out how to create that type of expression through a LambdaExpression. No matter what I try, the expression comes out as an ExpressionType.Call or ExpressionType.ArrayIndex. For ...

Lambda Expression Weirdness in Linq to SQL Where condition

Hi, I am working on an ASP.Net MVC application which uses the repository pattern with linq to sql as my data source. In my repository I expose the following method: public IEnumerable<T> Find(Expression<Func<T, bool>> where) { return _context.GetTable<T>().Where(where); } I am able to call this by saying: repository<User>.Find(u...

What exactly is the point of Expression Trees?

Ok, I just don't get it. I've read about as much as I can on the subject without knowing what it's all about: Why use Expression Trees? What's a real-world example of when and how I'd use them? What overall benefit(s) are there in using them? ...

Building a LINQ expression tree: how to get variable in scope

I'm building a LINQ expression tree but it won't compile because allegedly the local variable $var1 is out of scope: variable '' of type 'System.Object' referenced from scope '', but it is not defined This is the expression tree: .Block() { $var1; .If ($n.Property1 == null) { .Block() { $var1 = null; ...

Why do I get a null reference exception in this expression tree?

I have a tree expression that looks like this: .Block( System.Object $instance, MyType2 $result) { $result = (MyType2)((MyType1)$instance).Property1; .Goto return { }; .Label .LabelTarget useDefault:; $result = .Default(MyType2); .Label .LabelTarget return:; $result } These are the custom types ...

Why delegates generated dynamically from expressions are slower than hard-coded lambdas ?

I would have expected delegates generated from expression trees to achieve roughly the same performance as hard-coded, static, equivalent anonymous methods. However, it seems that dynamically generated delegates are noticeably slower... Here's a simple test program to illustrate the case. It just accesses 3 properties of an object 10000...

Linq Func/Expression Local Evaluation

Given this code: int min = 0; Expression<Func<List<IUser>, bool>> ulContainsJohn = (l => l.Where(u => u.FirstName == "John").Count() > min); Assert.AreEqual(true, ulContainsJohn.Compile()(userList)); min = 3; Assert.AreEqual(true, ulContainsJohn.Compile()(userList)); The...

Expression Trees

I have a method which have this signture public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) i use to pass lambda expressions and make search restriction in nhibernate by reteriving data from expressiontree so when class user pass something like : c => c.fullName == "John" && c.lastName == "...