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
31
votes
6 answers

Generate EF orderby expression by string

I want to generate expression by string parameter,some code like: private Expression> Generate(string orderby) { switch (orderby) { case "Time": return t => t.Time; case "Money": return…
yubaolee
  • 895
  • 2
  • 8
  • 16
30
votes
5 answers

How do I dynamically create an Expression> predicate?

How would I go about using an Expression Tree to dynamically create a predicate that looks something like... (p.Length== 5) && (p.SomeOtherProperty == "hello") So that I can stick the predicate into a lambda expression like…
Senkwe
  • 2,256
  • 3
  • 24
  • 33
29
votes
5 answers

Is it possible to interpret a C# expression tree to emit JavaScript?

For example, if you have an expression like this: Expression> fn = x => x * x; Is there anything that will traverse the expression tree and generate this? "function(x) { return x * x; }"
Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
28
votes
1 answer

Building a LINQ expression tree: how to get variable in scope

I'm building a LINQ expression tree but it won't compile because allegedly the local variable $var1 is out of scope: variable '' of type 'System.Object' referenced from scope '', but it is not defined This is the expression tree: .Block() { …
Sandor Drieënhuizen
  • 6,310
  • 5
  • 37
  • 80
27
votes
2 answers

Are LINQ expression trees proper trees?

Are LINQ expression trees proper trees, as in, graphs (directed or not, wikipedia does not seem too agree) without cycles? What is the root of an expression tree from the following C# expression? (string s) => s.Length The expression tree looks…
cynic
  • 5,305
  • 1
  • 24
  • 40
27
votes
3 answers

Serialize expression tree

I'm doing a distributed system in c# and have encountered a barrier. I need to be able to serialize Predicate with type Predicate> p = (entities => entities.OfType().Count() <= 3); I belive this is not possible in .net…
cholewa1992
  • 863
  • 1
  • 7
  • 16
27
votes
7 answers

Caching Compiled Expression tree

How to efficiently cache methods compiled from an expression tree ? public void SomeToStringCalls() { ToString(i => (i + 1).ToString(), 1); ToString(i => (i + 1).ToString(), 2); ToString(i => (i + 2).ToString(), 3); ToString(i => (i…
Toto
  • 7,491
  • 18
  • 50
  • 72
26
votes
6 answers

How to get the value of a ConstantExpression which uses a local variable?

I created an ExpressionVisitor implementation that overrides VisitConstant. However, when I create an expression that utilizes a local variable I can't seem to get the actual value of the variable. public class Person { public string FirstName…
devlife
  • 15,275
  • 27
  • 77
  • 131
26
votes
2 answers

C# compiler bug? Object initializer syntax used for write-only property in Expression makes csc crash

You may consider this a bug report, however I'm curious if I am terribly wrong here, or if there is an explanation from Eric or someone else at Microsoft. Update This is now posted as a bug on Microsoft Connect. Description Consider the following…
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
26
votes
1 answer

How Build Lambda Expression Tree with multiple conditions

Note: I know it's much simple to create this using dynamic linq but I want to learn. I want to create a lambda that "finds": Name=David AND Age=10. class Person { public int Age { get; set; } public string Name { get; set; } …
jullin
  • 633
  • 2
  • 12
  • 21
26
votes
2 answers

Is there a particular reason LinqKit's expander can't pick up Expressions from fields?

I'm using LinqKit library which allows combining expressions on the fly. This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and…
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
26
votes
4 answers

How can I get object instance from ()=>foo.Title expression

I have a simple class with a property class Foo { string Title { get; set; } } I am trying to simplify data binding by calling a function like BindToText(titleTextBox, ()=>foo.Title ); which is declared like void BindToText(Control…
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
26
votes
3 answers

How do I specify the object to return from an expression tree method?

I'm trying to create a method using an expression tree that returns an object, but I can't figure out how to actually specify the object to return. I've tried reading this, but the return value doesn't actually seem to be specified anywhere. I've…
thecoop
  • 45,220
  • 19
  • 132
  • 189
26
votes
5 answers

Performance of expression trees

My current understanding is that 'hard coded' code like this: public int Add(int x, int y) {return x + y;} will always perform better than expression tree code like this: Expression> add = (x, y) => x + y;…
cs0815
  • 16,751
  • 45
  • 136
  • 299
26
votes
1 answer

Understanding Expression.Invoke() Method

I've been understanding PredicateBuilder extension methods written Joseph Albahari and I saw this Expression.Invoke and honestly I couldn't understand the reason of it in the following method : public static Expression> Or (this…
Tarik
  • 79,711
  • 83
  • 236
  • 349