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
1
vote
1 answer
ExpressionTrees: getting a property name and its value
First I'll describe what I'm trying to achieve.
I want to create a method that grabs a property name and its value for logging purposes, so I have this:
public void Log(Expression> property,…

Evgeni
- 3,341
- 7
- 37
- 64
1
vote
1 answer
Expression trees for code which doesn't type check
Is it possible to construct an expression tree in .NET 3.5 from code like
Expression expr = () => (true + new object())
? It seems to be impossible, since "A compiler-generated expression tree is always rooted in a node of type Expression", but I…

Alexey Romanov
- 167,066
- 35
- 309
- 487
1
vote
1 answer
LINQ to Entities : "select new" expression tree throws System.NotSupportedException
I'm trying to build an Expression Tree that reflects a "select new" query.
I'm using Ethan's answer to this question.
It works great for common lists but with LINQ to Entities I get this exception:
System.NotSupportedException: Unable to create a…

andres.chort
- 836
- 7
- 9
1
vote
1 answer
how to convert parameterless constructor to MemberInitExpression
Can I create a MemberInitExpression or any expression of a parameterless constructor from a function that literally returns a parameterless constructor?
public IQueryable GetViewSelect(IQueryable myQ)
{
return…

sickdoctor
- 85
- 6
1
vote
1 answer
Negating a method call in an Expression tree
I'm generating a c# Linq expression dynamically as below, which will (in the example below) run string.Contains against the collection values.
var dynamicMethod = "Contains";
var parameter = Expression.Parameter(typeof (MyClass), "type");
var…

LJW
- 2,378
- 1
- 21
- 35
1
vote
1 answer
Using varying values in expressions in Entity Framework
This question was a inspired by the question: "Including a Method class inside an Expression". This question is not just "how can I find a solution to this" as that question already has that answer. My question is "How do I write a Expression that…

Scott Chamberlain
- 124,994
- 33
- 282
- 431
1
vote
1 answer
Is it possible to build and incrementally evaluate/mutate expression trees in Boost.Proto?
Is it possible to extract parts of a Boost.Proto expression tree, evaluate them individually (externally), and then mutate the expression tree, replacing the extracted parts with a result?
In my specific case, I'm trying to evaluate if I could…

meastp
- 682
- 1
- 7
- 15
1
vote
2 answers
How do you hold a reference to a NewExpression?
I have this code:
public static Func CreateBinder() {
NewExpression dataTransferObject = Expression.New(typeof(T).GetConstructor(Type.EmptyTypes));
ParameterExpression dataReader =…

sircodesalot
- 11,231
- 8
- 50
- 83
1
vote
1 answer
nHibernate HqlTreeBuilder to implement Linq methods for HierarchyId
I am looking at implementing a Hierarchy data structure in SQL Server using HierarchyId, and I need to add extension methods that can be used, via Linq, to use the HierarchyId methods exposed in TSQL. Now I have all the code for connecting a Linq…

Nick Albrecht
- 16,607
- 10
- 66
- 101
1
vote
0 answers
expression tree to source code conversion
Is there a library which converts arbitrary c# expression trees to string (representing C# source which would build up that expression tree)
var expTree = ...;
var stringRepresentation = Library.ToCSharpSource(expTree); // <- something like…

D.R.
- 20,268
- 21
- 102
- 205
1
vote
2 answers
Best way to determine if an expression is a constant expression
Which one do you prefer for testing if an expression is a ConstantExpression? From the NodeType property or a cast, and why?
public static bool IsConstantExpression(Expression expression)
{
return expression.NodeType ==…

Toto
- 7,491
- 18
- 50
- 72
1
vote
3 answers
Need help building LINQ to SQL Expression
I need to translate the following Code to an Expression and I will explain why:
results = results.Where(answer => answer.Question.Wording.Contains(term));
results is IQueryable
Question is ISurveyQuestion
Wording is String
The…

Joe Flateau
- 1,225
- 1
- 20
- 32
1
vote
2 answers
Create a reusable "meta-predicate" function accepting lambda operand parameters
Is it possible to create a "meta-predicate" by writing a a function that takes 2 (or 4 if necessary) lambdas which represent the left and right side properties (operands) and have it generate an predicate. Something like the following code…

j0tt
- 1,108
- 1
- 7
- 16
1
vote
2 answers
Lambda Generic Expression w/ Out Parameter
I am trying to use expressions w/ lambda delegates to obtain the name of the calling method but it is not formatting it properly.
Here is what I have so far: Question is.. how do I get what I am to expect similar to foo.Method.Name for both lambda…

Latency
- 426
- 3
- 12
1
vote
3 answers
Expression Tree and Compile method
This is all about the Compile method of Expression Type. Sorry being naïve since I am a late comer. I have been reading about building expression in order to enables dynamic modification of executable code. And It make sense for me when it comes to…

S.N
- 4,910
- 5
- 31
- 51