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
43
votes
5 answers

Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'

I created a method in C# to get methodname public string GetCorrectPropertyName(Expression> expression) { return ((MemberExpression)expression.Body).Member.Name; // Failure Point } and calling it as string lcl_name =…
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
41
votes
6 answers

How do I set a field value in an C# Expression tree?

Given: FieldInfo field = ; ParameterExpression targetExp = Expression.Parameter(typeof(T), "target"); ParameterExpression valueExp = Expression.Parameter(typeof(string), "value"); How do I compile a lambda…
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
39
votes
5 answers

What does Expression.Reduce() do?

I've been working with expression trees for a few days now and I'm curious to know what Expression.Reduce() does. The msdn documentation is not very helpful as it only states that it "reduces" the expression. Just in case, I tried an example (see…
d..
  • 1,063
  • 1
  • 10
  • 18
38
votes
3 answers

Working with nullable types in Expression Trees

I have an extension method to dynamically filter Linq to Entities results using string values. It works fine until I use it to filter nullable columns. Here's my code: public static IOrderedQueryable OrderingHelperWhere(this IQueryable
dstr
  • 8,362
  • 12
  • 66
  • 106
38
votes
1 answer

How does PredicateBuilder work

C# in a Nutshell has a free class called PredicateBuilder which constructs LINQ predicates piece by piece available here. Here's an extract of the method which adds a new expression to the predicate. Could someone explain it? (I have seen this…
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
37
votes
2 answers

C# 7.0 Value Tuple compile error?

When I am trying to compile the following code: var post = iPostService.GetAll().Select(x => (x.Title, x.Author)); I get the compiler error: 'An expression tree may not contain a tuple literal.' So I also tried this: var post =…
Tan Sang
  • 1,897
  • 1
  • 16
  • 28
36
votes
1 answer

Does Json.NET cache types' serialization information?

In .NET world, when it comes to object serialization, it usually goes into inspecting the object's fields and properties at runtime. Using reflection for this job is usually slow and is undesirable when dealing with large sets of objects. The other…
KFL
  • 17,162
  • 17
  • 65
  • 89
36
votes
2 answers

Given a type ExpressionType.MemberAccess, how do i get the field value?

I am parsing an Expression Tree. Given a NodeType of ExpressionType.MemberAccess, how do I get the value of that Field? From C# MSDN docs: MemberAccess is A node that represents reading from a field or property. A code snippet would be…
Keith Fitzgerald
  • 5,651
  • 14
  • 43
  • 58
34
votes
4 answers

Why would I want to use an ExpressionVisitor?

I know from the MSDN's article about How to: Modify Expression Trees what an ExpressionVisitor is supposed to do. It should modify expressions. Their example is however pretty unrealistic so I was wondering why would I need it? Could you name some…
t3chb0t
  • 16,340
  • 13
  • 78
  • 118
33
votes
7 answers

How set value a property selector Expression>

i need associate a entity property Address in my Person class entity with expressions linq in my FactoryEntities class using pattern factory idea, look this is what I have and I want to do: Address address = new Address(); address.Country =…
Emilio Montes
  • 355
  • 1
  • 3
  • 6
33
votes
2 answers

How to Combine two lambdas

Possible Duplicate: combining two lamba expressions in c# I have two following expressions: Expression> expr1 = s => s.Length == 5; Expression> expr2 = s => s == "someString"; Now I need to combine them with…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
33
votes
3 answers

Viewing the IL code generated from a compiled expression

Is it possible to view the IL code generated when you call Compile() on an Expression tree? Consider this very simple example: class Program { public int Value { get; set; } static void Main(string[] args) { var param =…
user47589
32
votes
3 answers

How to use Expression to build an Anonymous Type?

In C# 3.0 you can use Expression to create a class with the following syntax: var exp = Expression.New(typeof(MyClass)); var lambda = LambdaExpression.Lambda(exp); object myObj = lambda.Compile().DynamicInvoke(); But how do you use Expression to…
Flash
  • 1,615
  • 4
  • 17
  • 23
32
votes
8 answers

Is there a way to create a delegate to get and set values for a FieldInfo?

For properties there are GetGetMethod and GetSetMethod so that I can do: Getter = (Func)Delegate.CreateDelegate(typeof(Func), propertyInfo.GetGetMethod()); and Setter = (Action
nawfal
  • 70,104
  • 56
  • 326
  • 368
31
votes
5 answers

Performance of compiled-to-delegate Expression

I'm generating an expression tree that maps properties from a source object to a destination object, that is then compiled to a Func and executed. This is the debug view of the resulting…
JulianR
  • 16,213
  • 5
  • 55
  • 85