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
26
votes
3 answers

How to wrap Entity Framework to intercept the LINQ expression just before execution?

I want to rewrite certain parts of the LINQ expression just before execution. And I'm having problems injecting my rewriter in the correct place (at all actually). Looking at the Entity Framework source (in reflector) it in the end comes down to the…
Davy Landman
  • 15,109
  • 6
  • 49
  • 73
25
votes
2 answers

Java Expression Trees

Is there an equivalent of .net's Expression Trees that underly LINQ for the JVM? I would like to implement some LINQ like code structures in Scala and I am wondering if I have to roll my own expression tree library also. Update: I am not interested…
Steve Severance
  • 6,611
  • 1
  • 33
  • 44
24
votes
6 answers

Why is Func<> created from Expression> slower than Func<> declared directly?

Why is a Func<> created from an Expression> via .Compile() considerably slower than just using a Func<> declared directly ? I just changed from using a Func declared directly to one created from an…
MartinF
  • 5,929
  • 5
  • 40
  • 29
23
votes
3 answers

Why are lambda expression arguments ambiguous between Func and Expression?

Suppose I have a class: class MyClass { public int MyMethod(Func f) { return 0; } public int MyMethod(Expression> f) { return 1; } } When I try to call the method with a lambda expression, I get a compile error…
Jeffrey Sax
  • 10,253
  • 3
  • 29
  • 40
23
votes
7 answers

Is there a C# unit test framework that supports arbitrary expressions rather than a limited set of adhoc methods?

Basically NUnit, xUnit, MbUnit, MsTest and the like have methods similar to the following: Assert.IsGreater(a,b) //or, a little more discoverable Assert.That(a, Is.GreaterThan(b)) However, there are a limited number of such comparison operators…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
23
votes
2 answers

LINQ expressions. Variable 'p' of type referenced from scope, but it is not defined

I'm building a LINQ query dynamically with this code. It seems to work, but when i have more than one searchString in my search, (so when multiple expressions are added, i get the following error: Variable 'p' of type referenced from scope, but it…
Tys
  • 3,592
  • 9
  • 49
  • 71
23
votes
1 answer

How do I break down a chain of member access expressions?

The Short Version (TL;DR): Suppose I have an expression that's just a chain of member access operators: Expression> e = x => x.foo.bar.baz; You can think of this expression as a composition of sub-expressions, each comprising one…
22
votes
1 answer

How do I create a Linq expression tree with an F# lambda?

Here's what can be done in C# - var two = 2; System.Linq.Expressions.Expression> expr = x => x * two; expr.Compile().Invoke(4); // returns 8 I wish to do the precise equivalent in F#. Here's what I tried, but did not compile…
Bryan Edds
  • 1,696
  • 12
  • 28
22
votes
2 answers

Expression to create an instance with object initializer

Is there any way to create an instance of an object with object initializer with an Expression Tree? I mean create an Expression Tree to build this lambda: // my class public class MyObject { public bool DisplayValue { get; set; } } // my…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
21
votes
6 answers

C# LINQ to SQL: Refactoring this Generic GetByID method

I wrote the following method. public T GetByID(int id) { var dbcontext = DB; var table = dbcontext.GetTable(); return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) ==…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
21
votes
1 answer

Expressions breaking code when compiled using VS2015 Update 1

After installing Visual Studio 2015 Update 1 on my machine I saw that some of my unit tests failed. After doing some investigation I was able to reduce the problem to this line of code: Expression> expression = t => t.X == 0…
Devedse
  • 1,801
  • 1
  • 19
  • 33
21
votes
4 answers

Expression Trees and Invoking a Delegate

So I have a delegate which points to some function which I don't actually know about when I first create the delegate object. The object is set to some function later. I also then want to make an expression tree that invokes the delegate with an…
PythonPower
20
votes
2 answers

Expression of type 'System.DateTime' cannot be used for return type 'System.Object'

I've created an expression that I'm using for sorting which works fine, until I hit a DateTime field, where I get the following error (on the second line): Expression of type 'System.DateTime' cannot be used for return type …
Wildcat
  • 495
  • 2
  • 5
  • 9
20
votes
1 answer

Expression Trees and Nullable Types

I've been playing around with Expression Trees. I have the following simple method that performs a query by dynamically creating an Expression Tree. ItemType is a nullable int in the database, and also in the EF entity class. For some reason…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
20
votes
2 answers

LinqKit System.InvalidCastException When Invoking method-provided expression on member property

Given a simple parent/child class structure. I want to use linqkit to apply a child lambda expression on the parent. I also want the Lambda expression to be provided by a utility method. public class Foo { public Bar Bar { get; set;…
user293499
  • 203
  • 2
  • 4