expression-trees

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

When I first typed this question, I did so in order to find the duplicate questions, feeling sure that someone must have already asked this question. My plan was to follow those dupe links instead of posting this question. But this question has not been asked before as far as I can see ... it did not turn up in the "Related Questions" li...

how to change the type of the parameter in an Expression?

since i am using POCOS in my domain, i want my repository to be able to received Expression filters of the type of my POCOS and change the parameter in the expression to be the of type of my LINQ tables, my fields have the same name as my members so i was able to accomplish this for 1 and 2 lambda conditions by breaking into members and ...

Performing part of a IQueryable query and deferring the rest to Linq for Objects

I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects to process the rest of the Expression tree (for things like Joins, projection etc) My thought was that I could just replace the expression constant that contain...

Print out Linq Expression Tree Hierarchy

The DLR has some pretty cool code for Expression's, including some very nice code to print out Expression trees which I want to use so that: int a = 1; int b = 2; Expression<Func<int, int>> expression = (c) => a + (b * c) expression.Evaluate(5, stringBuilder) Outputs: (5) => a + (b * c) = 11 Where a = 1 b * c = 10 Where ...

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

I have a method that alters an "Account" object based on the action delegate passed into it: public static void AlterAccount(string AccountID, Action<Account> AccountAction) { Account someAccount = accountRepository.GetAccount(AccountID); AccountAction.Invoke(someAccount); someAccount.Save(); } This works as intended... AlterAc...

C# Linq-to-SQL: Refectoring this Generic GetByID method

I wrote the following method: public T GetByID(int id) { var dbcontext = DB; var table = dbcontext.GetTable<T>(); return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id); } Basically it's a method in a Generic class where T is a class in a DataContext. The m...

Hashing an expression tree

I am working on building some pseudo-intelligent caching into a LINQ query provider. What I'd like to do (ideally) is use the expression tree of a given query as the cache key in some scenarios. However, I don't want to store the entire object graph itself, so what's a quick way to get a hashsum-like value from an expression tree? Or if ...

How to convert an equation into formulas for individual variables?

How to convert an equation into formulas for individual variables? I am thinking about a math equations like: c^2 = a^2 + b^2 I would like to have a function that could process any formula, and give me the individual variable formulas. The above equation would produce the following: a = (c^2 - b^2)^0.5 b = (c^2 - a^2)^0.5 c = (a^2 +...

C#: How to parse arbitrary strings into expression trees?

In a project that I'm working on I have to work with a rather weird data source. I can give it a "query" and it will return me a DataTable. But the query is not a traditional string. It's more like... a set of method calls that define the criteria that I want. Something along these lines: var tbl = MySource.GetObject("TheTable"); tbl.Ad...

Why would you use Expression<Func<T>> rather than Func<T>?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>? ...

Should I use an expression parser in my Math game?

I'm writing some children's Math Education software for a class. I'm going to try and present problems to students of varying skill level with randomly generated math problems of different types in fun ways. One of the frustrations of using computer based math software is its rigidity. If anyone has taken an online Math class, you'll ...

How do I dynamically create an Expression<Func<MyClass, bool>> predicate?

Hi all, How would I go about using an Expression Tree to dynamically create a predicate that looks something like... (p.Length== 5) && (p.SomeOtherProperty == "hello") So that I can stick the predicate into a lambda expression like so... q.Where(myDynamicExpression)... I just need to be pointed in the right direction. Thanks. Ed...

Convert SQL Query back to Linq Expression programmatically

Is it possible to somehow programmatically convert a sql query to a linq expression tree? The sql query is supposed to be generated by this linq query, so I'm thinking of it as a poor man's serialization\deserialization of linq queries to t-sql format. Thank you. ...

Expression vs Predicate issues

I've got a bit of a challenge where I have to create an expression tree to represent a query input by the user. Since I don't have the time to create all the possible cases of user input, I figured that expression trees would aid me in solving this. For the most part, it has. I am, however, a bit stumped. I am in the code below trying t...

How do I make a LINQ expression to call a method?

I've been playing around with the DLR a bit and am a bit stuck on calling methods. For example, suppose I want to make an expression to push something onto a stack: class StackInfo{ protected Stack<SomeClass> _stack; public Expression Push(SomeClass item) { MethodInfo mi = _stack.GetType().GetMethod("Push"); ...

What is the difference between Converting and Unboxing?

In the DLR's LINQ Expressions, what is the difference between this: Expression.Convert(SomeVariableExpression, typeof(T)); and this: Expression.Unbox(SomeVariableExpression, typeof(T)); The documentation on this seems a bit sketchy. And more to the point, which one of these is equivalent to this C# code: (ClassA)InstanceOfClassB ...

How to create custom System.Linq.Expression classes in C#?

Is it possible to create custom Expression classes? If so, does anyone have any examples? Inheriting from the framework abstract Expression class poses a problem in that its constructor takes an ExpressionType parameter, which is a framework enum type - I obviously cannot customise this! Any ideas? ...

Expression tree modification problem

I have the following: Expression<Func<Car, int>> myExpr = car => car.Wheel.Tyre.Pressure; I want to remove the parameter, and make the first member the parameter for a sub-expression, so i end up with: Expression<Func<Wheel, int>> mySubExpr = wheel => wheel.Tyre.Pressure; This needs to work for any expression tree of the above form...

how to parse a tree data structure?

I have a tree data structure, comprised of nodes, that I need to parse into an expression tree. My nodes look like this (simplified): public class Node { public Node Left { get; set; } public Node Right { get; set; } public Operation OperationType { get; set; } public object Value { get; set; } ...

Stuck with BinaryExpression in C#

I want to implement Expression<Func<int, int, int>> Max = (p1,p2) => p1 > p2 ? p1:p2; as an expression tree and tried ParameterExpression LeftEx = Expression.Parameter(typeof(int), "p1"); ParameterExpression RightEx = Expression.Parameter(typeof(int), "p2"); BinaryExpression GroesserAls = Expression.GreaterThan(LeftEx, RightEx); Con...