expression-trees

Identify an event via a Linq Expression tree

The compiler usually chokes when an event doesn't appear beside a += or a -=, so I'm not sure if this is possible. I want to be able to identify an event by using an Expression tree, so I can create an event watcher for a test. The syntax would look something like this: using(var foo = new EventWatcher(target, x => x.MyEventToWatch) {...

Advice on C# Expression Trees

I'm working on a method that accepts an expression tree as a parameter, along with a type (or instance) of a class. The basic idea is that this method will add certain things to a collection that will be used for validation. public interface ITestInterface { //Specify stuff here. } private static void DoSomething<T>(Expression<Fun...

Assignment in .NET 3.5 expression trees

Is it possible to encode an assignment into an expression tree? ...

Serializing and Deserializing Expression Trees in C#

Is there a way to Deserialize Expressions in C#, I would like to store Expressions in a Database and load them at run time. ...

How do I build a Linq Expression Tree that compares against a generic object?

I have an IQueryable and an object of type T. I want to do IQueryable().Where(o => o.GetProperty(fieldName) == objectOfTypeT.GetProperty(fieldName)) so ... public IQueryable<T> DoWork<T>(string fieldName) where T : EntityObject { ... T objectOfTypeT = ...; .... return SomeIQueryable<T>().Where(o => o.GetProperty(fi...

Lambda Expression Tree Parsing

Thanks in advance guys. I am trying to use Lambda Expressions in a project to map to a third party query API. So, I'm parsing the Expression tree by hand. if I pass in a lambda expression like: p => p.Title == "title" everything works. however if my lambda expression looks like: p => p.Title == myaspdropdown.SelectedValue Using ...

Given a type ExpressionType.MemberAccess, how do i get the field value?

I am parsing an Expression Tree. Given a NodeType of ExpressionType.MemberAccess, how do I get the value of that Field? From C# MSDN docs: MemberAccess is A node that represents reading from a field or property. A code snippet would be incredibly, incredibly helpful. Thanks in advance!!! My code looks something like this: public ...

Are LINQ expression trees Turing complete?

As they are in .Net 3.5. I know they are in 4.0, as that's what the DLR works with, but I'm interested in the version we have now. ...

Expression inference during inheritance

I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program { public static void Main() { Descendant d = new Descendant(); d.TestMethod(); } } public class Base { protected void FigureItOut<TClass, TMember>(Expression<Func<TClass, TMember>> expr) {...

How do I create an expression tree to represent 'String.Contains("term")' in C#?

I am just getting started with expression trees so I hope this makes sense. I am trying to create an expression tree to represent: t => t.SomeProperty.Contains("stringValue"); So far I have got: private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue) { v...

Get the name of a property by passing it to a method

Hello, StackOverflow user jolson had a very nice piece of code that exemplifies how one can register menthods without using strings, but expression trees here. Is it possible to have something similar for properties instead of methods? To pass a property (not the name of the property) and inside the method to obtain the property name? ...

How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject { var param = Expression.Parameter(typeof(T), "o"); var body = Expression.PropertyOrField(param,columnName); va...

How do I generate a compiled lambda with method calls?

I'm generating compiled getter methods at runtime for a given member. Right now, my code just assumes that the result of the getter method is a string (worked good for testing). However, I'd like to make this work with a custom converter class I've written, see below, "ConverterBase" reference that I've added. I can't figure out how t...

Write stored procedures in Linq/Lambda (Unit-testable but performant stored procs). Possible???

I am creating a data source for reporting model (SQL Server Reporting Services). The reports requires a lot of joins and calculations (let's say, calculating financial parameters like money spent on this, that, amount A vs amount B)...all this involves subobjects. It makes a lot of sense to me to write unit tests for this code (i.e. wal...

How do I set a field value in an C# Expression tree?

Given: FieldInfo field = <some valid string field on type T>; ParameterExpression targetExp = Expression.Parameter(typeof(T), "target"); ParameterExpression valueExp = Expression.Parameter(typeof(string), "value"); How do I compile a lambda expression to set the field on the "target" parameter to "value"? ...

How do I create this expression tree in C#?

I am trying to create an expression tree that represents the following: myObject.childObjectCollection.Any(i => i.Name == "name"); Shortened for clarity, I have the following: //'myObject.childObjectCollection' is represented here by 'propertyExp' //'i => i.Name == "name"' is represented here by 'predicateExp' //but I am struggling w...

Expression trees in .NET - Libraries?

I have recently written a dynamic querying tool using expression trees and as I went the power of expression trees began to dawn on me. My querying tool could also form the basis of a reporting tool, a dynamic decision engine and maybe other cases where you need to work with dynamic objects in abstract ways. The process has been painfu...

How do I Create an Expression Tree by Parsing Xml in C#?

Hi, I am looking to create an expression tree by parsing xml using C#. The xml would be like the following: <Expression> <If> <Condition> <GreaterThan> <X> <Y> </GreaterThan> </Condition> <Expression /> <If> <Else> <Expression /> </Else> <Expression> or another example... <Expression> <Add> <X> ...

Best way to call an instance method in Expression Trees?

What is the best way to call an instance method within an Expression Tree? My current solution is something like this for an interface method "object GetRowValue(rowIndex)" of the interface IColumn. public static Expression CreateGetRowValueExpression( IColumn column, ParameterExpression rowIndex) { MethodIn...

Expression.AddChecked and System.Int16

Given two shorts (System.Int16) short left = short.MaxValue; short right = 1; I want to get an OverflowException when adding them. checked(left+right) does not work, because the result of left+right is an Int32. checked((short)(left+right)) works as expected. My problem is that, using Expression Trees, the "trick" doesn't work:...