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
20
votes
7 answers

C# switch in lambda expression

Is it possible to have a switch in a lambda expression? If not, why? Resharper displays it as an error.
Toto
  • 7,491
  • 18
  • 50
  • 72
19
votes
4 answers

.NET: Accessing non-public members from a dynamic assembly

I'm working on a library that allows users to input arbitrary expressions. My library then compiles those expressions as part of a larger expression into a delegate. Now, for still unknown reasons compiling the expression with Compile…
JulianR
  • 16,213
  • 5
  • 55
  • 85
19
votes
4 answers

Why are expression trees safer than reflection?

In this answer to the question of the fastest way to determine if a property contains a given attribute, user Darin Dimitrov posited that expression trees are safer than reflection. Is this true, and if so, why is it true?
Charles Y.
  • 395
  • 3
  • 14
19
votes
2 answers

Calling a Generic Method using Lambda Expressions (and a Type only known at runtime)

You can use Lambda Expression Objects to represent a lambda as an expression. How do you create a Lambda Expression Object representing a generic method call, if you only know the type -that you use for the generic method signature- at runtime? For…
SDReyes
  • 9,798
  • 16
  • 53
  • 92
19
votes
2 answers

Can I generate an async method dynamically using System.Linq.Expressions?

I know the compiler can't convert an async lambda expression to an expression tree, but is it possible to generate the expression tree manually ? var expr = Expression.Lambda>( // how do I use 'await' in the body here? ); var func =…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
19
votes
2 answers

C#: An item with the same key has already been added, when compiling expression

Ok, here's a tricky one. Hopefully there is an expression guru here who can spot what I am doing wrong here, cause I am just not getting it. I am building up expressions that I use to filter queries. To ease that process I have a couple of…
Svish
  • 152,914
  • 173
  • 462
  • 620
19
votes
5 answers

Efficiently eliminate common sub-expressions in .NET Expression Tree

I've written a DSL and a compiler that generates a .NET expression tree from it. All expressions within the tree are side-effect-free and the expression is guaranteed to be a "non-statement" expression (no locals, loops, blocks etc.). (Edit: The…
Ani
  • 111,048
  • 26
  • 262
  • 307
19
votes
3 answers

Get the parameter value from a Linq Expression

I have the following class public class MyClass { public bool Delete(Product product) { // some code. } } Now I have a helper class that looks like this public class Helper { public Type Type; public string…
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
18
votes
3 answers

How to create a Expression.Lambda when a type is not known until runtime?

This is best explained using code. I have a generic class that has a method that returns an integer. Here is a simple version for the purposes of explaining... public class Gen { public int DoSomething(T instance) { // Real code…
Phil Wright
  • 22,580
  • 14
  • 83
  • 137
18
votes
1 answer

What are some examples of MemberBinding LINQ expressions?

There are three possibilities, but I can't find examples: System.Linq.Expressions.MemberAssignment System.Linq.Expressions.MemberListBinding System.Linq.Expressions.MemberMemberBinding I want to write some unit tests to see if I can handle them,…
Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
18
votes
4 answers

Parsing and Translating Java 8 lambda expressions

In C# you can enclose a lambda expression in an expression tree object and then possibly parse it. I was wondering if this is also possible in Java? What I'm looking for is doing something like this: BooksRepository.getAll() .where(b -> b.getIban()…
Eyad
  • 327
  • 3
  • 10
17
votes
1 answer

How do I rewrite query expressions to replace enumerations with ints?

Inspired by a desire to be able to use enumerations in EF queries, I'm considering adding an ExpressionVisitor to my repositories that will take incoming criteria/specifications criteria and rewrite them to use the corresponding persisted int…
17
votes
9 answers

Whats a good use case for .net 4.0 Expression Trees?

This one was inspired by my language-guru co-worker who can't seem to find a good use for them, and after a few lame attempts of my own, I'd have to agree. Now I know these concepts tend to flow a lot more easily once you get some good practical…
KevinDeus
  • 11,988
  • 20
  • 65
  • 97
17
votes
1 answer

Expression.Bind() - what does it actually do?

So I've been playing with dynamically building expression trees lately and came across this method, which seems kinda odd. At first I thought "oh cool this is exactly what I need" after constantly writing code along the lines of var left = member…
Ilia G
  • 10,043
  • 2
  • 40
  • 59
17
votes
3 answers

Convert LINQ Expression to SQL Text without DB Context

Either LINQ to SQL or LINQ to Entities already have the ability to convert LINQ into a SQL text string. But I want my application to make the conversion without using the db context - which in turn means an active database connection - that both…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234