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
106
votes
2 answers

How to seed data with AddOrUpdate with a complex key in EF 4.3

I am trying to seed a development database with some test data. I have used context.People.AddOrUpdate(p => p.Id, people)); with much success. I have another table that I need to seed, in which I would not know the primary key. For example, I would…
84
votes
1 answer

Why are some object properties UnaryExpression and others MemberExpression?

Acting on the answer to my Select a model property using a lambda and not a string property name question, wanting to add properties to a collection as follows: var props = new ExportPropertyInfoCollection(); props.Include(model =>…
ProfK
  • 49,207
  • 121
  • 399
  • 775
51
votes
2 answers

How do I dynamically create an Expression> predicate from Expression>?

I trying to append where predicates and my goal is to create the same expression as: Services.Where(s => s.Name == "Modules" && s.Namespace == "Namespace"); I have the following code: Expression> sel1 = s =>…
Torbjörn Hansson
  • 18,354
  • 5
  • 33
  • 42
48
votes
1 answer

What is the purpose of LINQ's Expression.Quote method?

The MSDN documentation states: Expression.Quote Method Creates a UnaryExpression that represents an expression that has a constant value of type Expression. I've been able to build predicate expressions for use in LINQ queries by manually…
John Mills
  • 10,020
  • 12
  • 74
  • 121
31
votes
4 answers

Dynamic LINQ - Is There A .NET 4 Version?

I'm looking to use LINQ for some searching routines and wanted to have some dynamic where clauses. So, for example, if a user wants to search by city or search by state, I would have a dynamic LINQ Where<> call instead of creating two strongly…
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
25
votes
1 answer

How to convert a LambdaExpression to typed Expression>

I'm dynamically building linq queries for nHibernate. Due to dependencies, I wanted to cast/retrieve the typed expression at a later time, but I have been unsuccessfull so far. This is not working (the cast is supposed to happen elsewhere): var…
Larantz
  • 253
  • 1
  • 3
  • 5
17
votes
1 answer

How is a Func implicitly converted to Expression>?

I don't understand what is happening here: Both of these lines compile: Func func = () => new object(); Expression> expression = ()=>new object(); But this doesn't: expression = func; There isn't an implicit operator on…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
16
votes
1 answer

Exception using OrElse and AndAlso expression methods

I am trying to build an expression tree programmatically. I have in my input, a list of condition classes which have the following form: public class Filter { public string field { get; set; } public string operator { get; set; } public…
Lorenzo
  • 29,081
  • 49
  • 125
  • 222
16
votes
1 answer

LINQ Expression for Contains

I want to add dynamic expression in linq but facing issues on contains method it is working perfectly for Equal method Problem is i'm getting FilterField dynamically how to replace in query So far i had tried List Ids = new List(); …
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
16
votes
3 answers

How do I Emit a System.Linq.Expression?

I've got some code that generates various Func<> delegates using System.Linq.Expressions and Expression.Lambda>.Compile() etc. I would like to be able to serialize the generated functions into an assembly for later use. In the past I've done…
dkackman
  • 15,179
  • 13
  • 69
  • 123
16
votes
2 answers

How do I compose Linq Expressions? ie Func>, Exp>, Exp>>

I'm creating a Validator class. I'm attempting to implement the Linq SelectMany extension methods for my validator to be able to compose expressions using a Linq query and validate the final result even when the underlying values change. The…
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
15
votes
3 answers

Entity Framework filter data by string sql

I am storing some filter data in my table. Let me make it more clear: I want to store some where clauses and their values in a database and use them when I want to retrieve data from a database. For example, consider a people table (entity set) and…
ConductedClever
  • 4,175
  • 2
  • 35
  • 69
15
votes
2 answers

How can I use a Predicate in an EF Where() clause?

I'm trying to use predicates in my EF filtering code. This works: IQueryable filtered = customers.Where(x => x.HasMoney && x.WantsProduct); But this: Predicate hasMoney = x => x.HasMoney; Predicate wantsProduct = x =>…
Bobby B
  • 2,287
  • 2
  • 24
  • 47
14
votes
2 answers

Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

i have one common grid view column filter method that filter grid view record with ColumnName and SearchText wise. here when i operate on nullable int datacolumn there is error thrown from this method like : Expression of type 'System.Int32' cannot…
Shalin Gajjar
  • 229
  • 1
  • 5
  • 15
14
votes
2 answers

Dynamic linq order by on nested property with null properties

I'm using this dynamic linq orderby function which I got from here. This works fine with nested properties so I could do this: var result = data.OrderBy("SomeProperty.NestedProperty"); The problem is that if SomeProperty is null then performing the…
user2535425
1
2 3
39 40