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.)

1973 questions
1021
votes
10 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
  • 10,764
  • 3
  • 19
  • 16
542
votes
22 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
  • 14,711
  • 10
  • 43
  • 75
125
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
  • 3,997
  • 3
  • 27
  • 33
100
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
  • 61,190
  • 29
  • 155
  • 224
97
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
  • 151,500
  • 20
  • 287
  • 393
94
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,239
  • 12
  • 47
  • 68
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,665
  • 6
  • 41
  • 88
88
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,049
  • 3
  • 29
  • 38
83
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
70
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
  • 20,962
  • 21
  • 90
  • 152
70
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
  • 14,711
  • 10
  • 43
  • 75
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…
Donny
  • 4,738
  • 8
  • 35
  • 50
67
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,017
  • 24
  • 77
  • 94
61
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
  • 12,828
  • 7
  • 49
  • 65
56
votes
3 answers

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…
Charlie Flowers
  • 16,782
  • 8
  • 69
  • 86
1
2 3
99 100