Questions tagged [expression-trees]

Expression Trees are an abstract representation of code in a tree structure where each node of the tree represents a programming construct (conditional, assignment, method call, etc.)

2090 questions
1136
votes
12 answers

Why would you use Expression> rather than Func?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression> rather than a plain old Func?
Richard Nagle
  • 11,974
  • 3
  • 21
  • 16
593
votes
23 answers

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string.…
Schotime
  • 15,707
  • 10
  • 46
  • 75
127
votes
1 answer

Are Roslyn SyntaxNodes reused?

I've been taking a look to Roslyn CTP and, while it solves a similar problem to the Expression tree API, both are immutable but Roslyn does so in a quite different way: Expression nodes have no reference to the parent node, are modified using a…
Olmo
  • 4,257
  • 3
  • 31
  • 35
105
votes
4 answers

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

I have the following generic extension method: public static T GetById(this IQueryable collection, Guid id) where T : IEntity { Expression> predicate = e => e.Id == id; T entity; // Allow reporting more…
Steven
  • 166,672
  • 24
  • 332
  • 435
103
votes
5 answers

What does Expression.Quote() do that Expression.Constant() can’t already do?

Note: I am aware of the earlier question “What is the purpose of LINQ's Expression.Quote method?”, but if you read on you will see that it doesn’t answer my question. I understand what the stated purpose of Expression.Quote() is. However,…
Timwi
  • 65,159
  • 33
  • 165
  • 230
97
votes
7 answers

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.
Alexandre Brisebois
  • 6,599
  • 12
  • 50
  • 69
91
votes
4 answers

Compiled C# Lambda Expressions Performance

Consider the following simple manipulation over a collection: static List x = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var result = x.Where(i => i % 2 == 0).Where(i => i > 5); Now let's use Expressions. The following code is roughly…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
91
votes
8 answers

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

public static IQueryable ApplySortFilter(this IQueryable query, string columnName) where T : EntityObject { var param = Expression.Parameter(typeof(T), "o"); var body = Expression.PropertyOrField(param,columnName); …
JTew
  • 3,149
  • 3
  • 31
  • 39
84
votes
7 answers

Expression trees for dummies?

I am the dummy in this scenario. I've tried to read on Google what these are but I just don't get it. Can someone give me a simple explanation of what they are and why they're useful? edit: I'm talking about the LINQ feature in .Net.
Dave
74
votes
6 answers

What's the purpose of the Expression class?

I'm wondering what exactly is the difference between wrapping a delegate inside Expression<> and not ? I'm seeing Expression being used a lot with LinQ, but so far I've not found any article that explains the difference between this, and just…
Steffen
  • 13,648
  • 7
  • 57
  • 67
74
votes
4 answers

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
flesh
  • 23,725
  • 24
  • 80
  • 97
74
votes
9 answers

Access the value of a member expression

If i have a product. var p = new Product { Price = 30 }; and i have the following linq query. var q = repo.Products().Where(x=>x.Price == p.Price).ToList() In an IQueryable provider, I get a MemberExpression back for the p.Price which contains a…
Schotime
  • 15,707
  • 10
  • 46
  • 75
71
votes
7 answers

Practical use of expression trees

Expression trees are a nice feature, but what are its practical uses? Can they be used for some sort of code generation or metaprogramming or some such?
Dmitri Nesteruk
  • 23,067
  • 22
  • 97
  • 166
69
votes
1 answer

What are Expression Trees, how do you use them, and why would you use them?

I just came across the concept of expression trees which I have heard multiple times. I just want to understand what is meant by an expression tree and its purpose. I would love it if someone could also direct me to simple explanations and samples…
Donald N. Mafa
  • 5,131
  • 10
  • 39
  • 56
67
votes
1 answer

PropertyExpression is missing

I try to write a simple example using Expressions, but have a strange bug: I can't use PropertyExpression at compile time. When I write it I get an error and it doesn't compile But in runtime at breakpoint I can write var runtimeBody =…
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
1
2 3
99 100