expression-trees

Linq expression tree string comparison

I am trying to construct an expression tree to operate against a string array. I need to figure out how to use the Equal method. Could anyone give me an example of 1) using the Expression.Equal (or .Equals) method to compare a string to a constant, and 2) using an Expression of whatever sort to use the string.Contains() method for filt...

Dynamic built Linq to SQL Query

Hi, i want to build a generic search window using linq to sql. This is what i was trying to do: class SearchWindow<T> : Form : Where T: class { public SearchWindow(Func<T, string> codeSelector, Func<T, string> nameSelector) { var db = new DataContext(); var table = db.GetTable<T>(); ...

.NET 4: Expression/Statement trees

Updated Question Further Down I've been experimenting with expression trees in .NET 4 to generate code at runtime and I've been trying to implement the foreach statement by building an expression tree. In the end, the expression should be able to generate a delegate that does this: Action<IEnumerable<int>> action = source => { var...

How do I subscribe to an event of an object inside an expression tree?

Sorry I couldn't think of a better title. This is a two part question that only make sense together. Say I have a constructor like this public Fact(INotifyPropertyChanged observable, Func<bool> predicate) { this.predicate = predicate; observable.PropertyChanged += (sender, args) => PropertyChanged(this, new Prope...

C# Expression syntax

I was thinking about the difference between Expression<Func<>> and Func<>, and wondered if you could convert a static method to an expression tree as follows: class Program { static void Main(string[] args) { Func<int, int> t = x => hrm(x); Func<int, int> t2 = new Func<int, int>(hrm); // Works as expecte...

More on casting Func<T,T> and Expression<Func<T,T>>

Hi there, I've spent hours with this but haven't managed... Please see example below - How can this be done? The idea is to build a compiled expression of type Func<dynamic, dynamic> given an Expression<Func<T1,T2>> passed by the class' consumer. I have been able to solve this problem (thanks to SO) IF the types T1 and T2 are known at...

.NET 4.0: What does Expression.Reduce() do?

Hi again, I've been working with expression trees for a few days now and I'm curious to know what Expression.Reduce() does. The msdn documentation is not very helpful as it only states that it "reduces" the expression. Just in case, I tried an example (see below) to check if this method included mathematical reduction, but this doesn't ...

.NET 4.0: How to create an Expression<Func<dynamic, dynamic>> - Or is it a bug?

Hi All, During my work with expression trees for a few days now, I came across something that I find difficult to understand; hopefully someone will be able so shed some light here. If you code Expression<Func<dynamic, dynamic>> expr1 = x => 2 * x; the compiler will complain and you won't get anywhere. However, it seems that if you cre...

C#: Is it possible to check that a simple expression will always return true?

And I mean this in the easiest way. Say you have a function with the following signature: public static Expression<Func<T, bool>> CreateExpression<T>(string value) { // ... } Usually it will create a more complex expression of some sort, but if the value is null the method should return a constant, always true expression. In other...

Expression tree - how to get at declaring instance?

I'm a newbie when it comes to expression trees, so I'm not sure how to ask this question or what terminology to use. Here's an overly-simplifed version of what I'm trying to do: Bar bar = new Bar(); Zap(() => bar.Foo); public static void Zap<T>(Expression<Func<T>> source) { // HELP HERE: // I want to get the bar instance and call...

Dynamic Linq 2 Sql using Expressions Trees raising exception "Binary Operator LessThan not defined for System.String and System.String"

Hi, I'm trying to write a dynamic Linq 2 Sql query using Expressions trees but I'm getting a exception telling me that the LessThan and GreaterThan operators are not defined for System.String and System.String, which i find odd, is that true? or am I doing something wrong? Expression<Func<SomeDataContextType, string>> codeSelectorExpres...

Expression to Call a Method on Each Property of a Class

I want to take a class, loop through it's properties, get the property value, and call a method passing that property value in. I think I can get the property values, but what does the lambda expression's body look like? What body is used to call a method on each property? This is what I have so far... Action<T> CreateExpression<T>( T...

Expression Trees in .NET 4.0: Expression.Call fails to find method "get_Item" in type List<T>

Hi there, I'm stuck with the problem below and wondering is someone out there will be able to help. I have added comments to the code to make it self-explanatory but let me know if you need more info or if the problem is unclear. Thanks a lot in advance! Edit: I've been asked to summarize the problem in text, so here it goes: under th...

What is the algorithm for parsing expressions in infix notation?

I would like to parse boolean expressions in PHP. As in: A and B or C and (D or F or not G) The terms can be considered simple identifiers. They will have a little structure, but the parser doesn't need to worry about that. It should just recognize the keywords and or not ( ). Everything else is a term. I remember we wrote simple ari...

C#: An item with the same key has already been added, when compiling expression

Ok, here's a tricky one. Hopefully there is an expression guru here who can spot what I am doing wrong here, cause I am just not getting it. I am building up expressions that I use to filter queries. To ease that process I have a couple of Expression<Func<T, bool>> extension methods that makes my code cleaner and so far they have been w...

Can LINQ To SQL generate invalid SQL?

I have two tables that I am using Linq to SQL with. The tables have a 1 to many association. The important part of the database schema is as follows: Camera: Id (int) SerialNumber (string) ... CameraCalibration Id (int) CameraFk (int) ... Using LINQ to SQL I can obtain a list of all Camera Calibrations for cameras with 10...

Transform a delegate to a Expression Tree

Hello, I wonder if you can create and modify a Expression Tree out of a existing delegate. Kinda like public void Foo() { Console.WriteLine(1000); } .... Expression exp = Foo.GetExpression(); //Now do something what changes 1000 to 2000... So I would like to reverse engineer a allready excisting Method. My problem is that I...

Python: Optimizing, or at least getting fresh ideas for a tree generator.

I have written a program that generates random expressions and then uses genetic techniques to select for fitness. The following part of the program generates the random expression and stores it in a tree structure. As this can get called billions of times during a run, I thought it should be optimized for time. I'm new to programming...

how to set value for this property public Expression<Action<Controller>> Action { get; set; }

i have a class that has this property public Expression<Action<Controller>> Action { get; set; } how to set it's value for example: var x = new MyClass{ Action = What_To_Write_here } ...

how to get parameter names from an expression tree?

I have a expression of this type: Expression<Action<T>> expression how do I get the parameters names from this expression (optional: and values) ? example: o => o.Method("value1", 2, new Object()); names could be str_par1, int_par2, obj_par3 ...