Questions tagged [linq-expressions]

An API for composing code with declarative expression trees, used by LINQ and the DLR.

About

The System.Linq.Expressions namespace in .NET 3.5 and later provides an API for composing code in terms of declarative expression trees, as well as a compiler to transform top-level lambda expressions into delegates (i.e., callbacks). Originally created to facilitate the creation of custom query providers in , it has since evolved to form the basis of the .

Relationship to LINQ

The first version of System.Linq.Expressions, which shipped with .NET 3.5, was designed to model query expressions. Consequently, it supports only a subset of the expression types representable in .NET languages like C# and VB.NET. Query expressions in LINQ are processed by a query provider and, ideally, transformed into a combination of local and remote operations. The LINQ to SQL and LINQ to Entity Framework providers, for example, will attempt to transform a query expression into equivalent SQL, enabling operations like filtering, sorting, and aggregation to be performed (and optimized) by the database engine. This is particularly useful for larger data sets that would render in-memory processing impractical.

Evolution

While the System.Linq.Expressions APIs proved useful for modeling complex expressions, it lacked the control flow mechanisms necessary to model the kind of code produced by imperative programming languages. Thus, it was less useful for general-purpose code generation. This changed when Microsoft began developing its Dynamic Language Runtime (DLR), a set of common language services designed to support the development of dynamic languages.

Core aspects of the DLR include support for dynamic call sites, dynamic code generation, and language hosting. Microsoft decided to use System.Linq.Expressions as the code model for the DLR's dynamic call site and code generation infrastructure. The expression tree API and compiler were extended to support a much wider range of language constructs, most notably control flow (conditions, loops, switch statements, etc.) and exception handling. Languages like IronRuby and IronPython ultimately transform their own syntax trees into LINQ expression trees, which may then be compiled or interpreted directly by the DLR.

Developers wishing to perform runtime code generation may choose to compose code using LINQ/DLR expression trees as a more convenient and less error-prone alternative to emitting raw IL byte code.

591 questions
14
votes
1 answer

Pass LINQ expression to another QueryProvider

I have a simple custom QueryProvider that takes an expression, translates it to SQL and queries an sql database. I want to create a small cache in the QueryProvider that stores commonly accessed objects so retrieval can happen without a database…
Anthony
  • 850
  • 8
  • 20
12
votes
4 answers

Converting a lambda expression into a unique key for caching

I've had a look at other questions similar to this one but I couldn't find any workable answers. I've been using the following code to generate unique keys for storing the results of my linq queries to the cache. string key =…
James South
  • 10,147
  • 4
  • 59
  • 115
12
votes
1 answer

How do I access a Dictionary Item using Linq Expressions

I want to build a Lambda Expression using Linq Expressions that is able to access an item in a 'property bag' style Dictionary using a String index. I am using .Net 4. static void TestDictionaryAccess() { ParameterExpression…
Michael Dausmann
  • 4,202
  • 3
  • 35
  • 48
12
votes
1 answer

Switch without cases (but with default) in System.Linq.Expressions

I have tried to create a switch expression with System.Linq.Expressions: var value = Expression.Parameter(typeof(int)); var defaultBody = Expression.Constant(0); var cases1 = new[] { Expression.SwitchCase(Expression.Constant(1),…
Viacheslav Ivanov
  • 1,535
  • 13
  • 22
12
votes
4 answers

Dynamically add new lambda expressions to create a filter

I need to do some filtering on an ObjectSet to obtain the entities I need by doing this : query = this.ObjectSet.Where(x => x.TypeId == 3); // this is just an example; Later in the code (and before launching the deferred execution) I filter the…
user2324540
12
votes
1 answer

Reliably detecting compiler generated classes in C# expression trees

I'm building a C# expression-to-Javascript converter, along the lines of Linq-to-SQL, but I'm running into problems with compiler generated expression trees. The particular problem I'm having is dealing with MemberExpression values which were…
Rafe
  • 5,237
  • 3
  • 23
  • 26
11
votes
3 answers

How do Linq Expressions determine equality?

I am considering using a Linq Expression as a key in a dictionary. However, I am concerned that I will get strange results, because I don't know how Equality is determined by Linq expressions. Does a class derived from Expression compare value…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
11
votes
1 answer

variable 'x' of type 'Product' referenced from scope, but it is not defined

I have a class named Product in class library project. I am using SubSonic SimpleRepository to persist objects. I have a method as follows in Product class: public static IList Load(Expression> expression) { var…
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
11
votes
2 answers

Getting ConstantExpression.Value when actual value wrapped into DisplayClass because of closure

Below is a simple demonstration code of my problem. [TestClass] public class ExpressionTests { [TestMethod] public void TestParam() { Search(s => s.Id == 1L); GetStudent(1L); } private void…
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
11
votes
1 answer

Extracting Func<> from Expression<>

I wanna extract the Func<> from the following Expression : Expression, IOrderedQueryable>> order = q => q.OrderByDescending(c=>c.FullName); Func, IOrderedQueryable> orderFunc = ? How can I…
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
11
votes
2 answers

Dynamically get class attribute value from type

I'm trying to write a method that finds all types in an assembly with a specific custom attribute. I also need to be able to supply a string value to match on. The caveat is that I would like to be able to run this on any class and return any…
Andrew Munro
  • 172
  • 1
  • 1
  • 9
10
votes
3 answers

Combine several similar SELECT-expressions into a single expression

How to combine several similar SELECT-expressions into a single expression? private static Expression> CombineSelectors(params Expression>[] selectors) { // ??? return null; …
Boris Mitchenko
  • 880
  • 9
  • 18
10
votes
1 answer

Why Is a Compiled Delegate Faster Than a Declared Delegate?

To start with, this is not the same as Why is Func<> created from Expression> slower than Func<> declared directly? and is surprisingly just the opposite of it. Additionally, all links and questions that I have found while researching this issue…
Mike-E
  • 2,477
  • 3
  • 22
  • 34
10
votes
1 answer

How to set a breakpoint in a lambda expression?

I would like to debug a lambda that is called in an expression tree. Unfortunately, the breakpoint is never hit. Here's a full Console Program to play with: private static void Main() { var evalAndWrite = EvalAndWrite(x => x + 1 /* a breakpoint…
Dejan
  • 9,150
  • 8
  • 69
  • 117
10
votes
1 answer

Convert Linq expression "obj => obj.Prop" into "parent => parent.obj.Prop"

I have an existing expression of type Expression>; it contains values like cust => cust.Name. I also have a parent class with a field of type T. I need a method that accepts the above as a parameter and generates a new expression…
Daniel
  • 1,695
  • 15
  • 33
1
2
3
39 40