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

Dynamic Func, IOrderedQueryable> Expression

I am using patterns mentioned here http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application And i am using method below to query EF public virtual…
bahadir arslan
  • 4,535
  • 7
  • 44
  • 82
9
votes
5 answers

Dynamic Expression using LINQ. How To Find the Kitchens?

I try do implement a user dynamic filter, where used selects some properties, selects some operators and selects also the values. As I didn't find yet an answer to this question, I tried to use LINQ expressions. Mainly I need to identify all houses…
serhio
  • 28,010
  • 62
  • 221
  • 374
9
votes
4 answers

How to create an Expression builder in .NET

I have written some code to allow filtering of products on our website, and I am getting a pretty bad code smell. The user can select 1-* of these filters which means I need to be specific with the WHERE clause. I think I am looking for a way to…
aspirant_sensei
  • 1,568
  • 1
  • 16
  • 36
9
votes
2 answers

How to log a predicate Expression?

Typically my repositories have logging statements for debugging purposes, allowing me to see values of parameters. Recently I went down the path of creating a generic repository that take predicate Expression as argument for great flexibility,…
e36M3
  • 5,952
  • 6
  • 36
  • 47
9
votes
2 answers

LINQ Expression> equavalent of .Contains()

Has anybody got an idea of how to create a .Contains(string) function using Linq Expressions, or even create a predicate to accomplish this public static Expression> Or(this Expression> expr1, Expression
BK.
  • 365
  • 4
  • 13
9
votes
3 answers

ReflectedType from MemberExpression

Given this code: static void Main(string[] args) { Expression> test = i => i.Prop; var body = (UnaryExpression) test.Body; Console.WriteLine(((MemberExpression)…
asgerhallas
  • 16,890
  • 6
  • 50
  • 68
9
votes
2 answers

How to convert between Linq expressions with different return types?

I'm having a headache trying to convert the following linq expression. Expression> to the following linq expression... Expression> In the example above the object is always of type U. I know how easy it could to…
Leo
  • 14,625
  • 2
  • 37
  • 55
8
votes
3 answers

Expression tree for String.IndexOf method

How should I construct Expression tree for string.IndexOf("substring", StringComparison.OrdinalIgnoreCase)? I can get it working without the second argument: StringComparison.OrdinalIgnoreCase. These are my attempts so far: var methodCall = typeof…
ashokgelal
  • 80,002
  • 26
  • 71
  • 84
8
votes
2 answers

Calling (params object[]) with Expression[]

I'm trying to call String.Format from with in a Linq.Expression tree. Here's a quick example: var format = Expression.Constant("({0}) {1}"); var company = Expression.Property(input, membernames.First()); var project =…
Timothy Baldridge
  • 10,455
  • 1
  • 44
  • 80
8
votes
1 answer

System.Core error: "Code supposed to be unreachable" using C# Entity Framework and Linq with Expression

I'm getting a "Code supposed to be unreachable" error when executing the following Linq to Sql statement. I'm using EF 6.1.3. I think this is a known bug related to filtering a navigation property. It seems like it might be fixed in EF7 but I don't…
8
votes
0 answers

Expression.Call() overload for calling an instance method expecting a single argument is missing. Is there a reason for this?

Looking at the documented overloads available for the Expression.Call(), method, I can find the following overloads to obtain an expression node that will perform a call to an instance method expecting: no arguments two arguments three…
BlueStrat
  • 2,202
  • 17
  • 27
8
votes
2 answers

What is the motivation of C# ExpressionVisitor's implementation?

I have to design a solution for a task, and I would like to use something theoretically similar to C#'s ExpressionVisitor. For curiosity I opened the .NET sources for ExpressionVisitor to have a look at it. From that time I've been wondering why the…
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
8
votes
2 answers

What does it mean for a label target to receive a value?

I have a few questions about the System.Linq.Expressions.LabelExpression and its ancillary classes and methods. 1) The documentation for the LabelExpression class reads thus: Represents a label, which can be put in any Expression context. If it is…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
7
votes
1 answer

How to make LINQ-to-Objects handle projections?

I have implemented a basic (naive?) LINQ provider that works ok for my purposes, but there's a number of quirks I'd like to address, but I'm not sure how. For example: // performing projection with Linq-to-Objects, since Linq-to-Sage won't handle…
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
7
votes
4 answers

Changing the return type of an expression>

Say I have an Expression> is it possible to dynamically change the return type based on a Type variable to be something like Expression> I have the following class: public class ImportCheck { public int id { get;…
Gaz
  • 1,249
  • 3
  • 19
  • 37
1 2
3
39 40