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.)
Questions tagged [expression-trees]
2090 questions
58
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
- 17,338
- 10
- 71
- 88
58
votes
5 answers
Internal .NET Framework Data Provider error 1025
IQueryable query = context.Organizations;
Func predicate = r => !r.IsDeleted;
query.Select(o => new {
Reservations = o.Reservations.Where(predicate)
}).ToList();
this query throws "Internal .NET Framework…

Freshblood
- 6,285
- 10
- 59
- 96
53
votes
1 answer
Expression.Lambda and query generation at runtime, simplest "Where" example
I was trying to generate a simple Lambda Expression at runtime with no luck... something like this:
var result = queryableData.Where(item => item.Name == "Soap")
Here is my example class and a fixture queryable:
public class Item
{
public int…

CodeAddicted
- 979
- 2
- 10
- 13
53
votes
3 answers
Construct LambdaExpression for nested property from string
I am trying to create a lambda expression for a nested property at run-time from the name of the propert. Basically I am trying to create the lambda expression specified by:
var expression = CreateExpression(foo =>…

Seph
- 8,472
- 10
- 63
- 94
51
votes
2 answers
How do I dynamically create an Expression> predicate from Expression>?
I trying to append where predicates and my goal is to create the same expression as:
Services.Where(s => s.Name == "Modules" && s.Namespace == "Namespace");
I have the following code:
Expression> sel1 = s =>…

Torbjörn Hansson
- 18,354
- 5
- 33
- 42
51
votes
6 answers
Mutating the expression tree of a predicate to target another type
Intro
In the application I 'm currently working on, there are two kinds of each business object: the "ActiveRecord" kind and the "DataContract" kind. So for example, there would be:
namespace ActiveRecord {
class Widget {
public int Id {…

Jon
- 428,835
- 81
- 738
- 806
51
votes
1 answer
Why can't an expression tree contain a named argument specification?
Using AutoMapper, I hit a place where a named argument would've fit very nicely:
.ForMember(s => s.MyProperty, opt => opt.MapFrom(s => BuildMyProperty(s, isAdvanced: false)))
But the compiler yelled at me:
An expression tree may not contain a…

Brandon Linton
- 4,373
- 5
- 42
- 63
50
votes
3 answers
C# 4 "dynamic" in expression trees
I'm trying to figure out how to put all the pieces together, and would appreciate a concrete source code sample for a simple case to start with.
Consider the following C# code:
Func f = (x, y) => x + y;
I can produce an equivalent…

Pavel Minaev
- 99,783
- 25
- 219
- 289
50
votes
3 answers
variable '' of type '' referenced from scope '', but it is not defined
Well, the following code is self-explaining; I want to combine two expressions into one using And operator. The last line causes rune-time the error:
Additional information: variable 'y' of type 'System.String' referenced from scope '', but it is…

Hans
- 2,674
- 4
- 25
- 48
49
votes
2 answers
Expression tree differences between C# and VB.Net
I have a library working on expression trees. The library need to work with both C# and VB.Net
Noticed some differences between the languages on how the expression trees are constructed
String comparison
() => "a" == "b" becomes…

adrianm
- 14,468
- 5
- 55
- 102
49
votes
4 answers
Lambda to Expression tree conversion
I will keep it really simple,
How do I get expression tree out of lambda??
or from query expression ?

Prashant Cholachagudda
- 13,012
- 23
- 97
- 162
48
votes
9 answers
How to create LINQ Expression Tree to select an anonymous type
I would like to generate the following select statement dynamically using expression trees:
var v = from c in Countries
where c.City == "London"
select new {c.Name, c.Population};
I have worked out how to generate
var v = from c in…

Tom Deloford
- 2,055
- 1
- 23
- 24
45
votes
6 answers
Getting the object out of a MemberExpression?
So, lets say I have the following expression in C#:
Expression> expr = () => foo.Bar;
How do I pull out a reference to foo?

Brian Genisio
- 47,787
- 16
- 124
- 167
43
votes
2 answers
How do I create an expression tree calling IEnumerable.Any(...)?
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…

flesh
- 23,725
- 24
- 80
- 97
43
votes
2 answers
Error in C#: "an expression tree may not contain a base access" - why not?
I was calling a method that accepts Expression>.
As part of the expression I was passing:
this.Bottom == base.lineView.Top
The compiler gave me an error that
an expression tree may not contain a base access
So I simply changed it to…

Krumelur
- 32,180
- 27
- 124
- 263