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

LINQ to Entities - Generate where predicate dynamically

I have this: public void AssertReadWorks( IRepository repository, T entity, Expression> keyComparer) where T : class { entity = repository.GetAll().Single(x => x.Id == entity.Id); } [TestMethod] public void…
pinkfloydhomer
  • 873
  • 3
  • 9
  • 16
0
votes
2 answers

Dynamic Data - Implementing IQueryable for List Contains()

I have a series of objects that all have a similar property that is a List of the Ids of the groups to which they belong (many parents per child). I'm having trouble programatically implementing the Linq Expression necessary to make this filter work…
Sonny Boy
  • 7,848
  • 18
  • 76
  • 104
0
votes
1 answer

ExpressionVisitor.Visit throws 'must be reducible node' error in Data Service Query Visitor

I've got a problem with an implementation of the repository pattern for my WCF Data Service. To sum up, I'm trying to use a repository pattern within a client application that utilizes a plugable model for the repositories that it requires. The root…
The Senator
  • 5,181
  • 2
  • 34
  • 49
0
votes
0 answers

ExpressionVisitor with Multiple Types

I am using ExpressionVisitor to convert type of Expression> to Expression> and it works very well. But i noticed that if expression contains another types because of navigation properties ExpressionVisitor cause…
bahadir arslan
  • 4,535
  • 7
  • 44
  • 82
0
votes
1 answer

The LINQ expression node type 'Invoke' is not supported in LINQ to Entities

I have a problem trying to implement a filtering expression to filter a list of entities : The LINQ expression node type 'Invoke' is not supported in LINQ to Entities. This is the code : public IList
user2219884
0
votes
2 answers

How to use the value from a property accessor LINQ expression in a Contains LINQ expression?

I currently have a LINQ expression for a property accessor that accesses a property on an object. I now need to build a LINQ expression that will evaluate a predicate to see if the result from the property accessor is contained within a list. I've…
AJ Henderson
  • 1,120
  • 1
  • 12
  • 32
0
votes
3 answers

A List of Field Names as a String Array - LINQ Expressions

Hello MVC and LINQ Experts, I have a Model that looks like this: public class SomeClass : IValidatableObject { public string SomeString { get; set; } public string SomeString2 { get; set; } public int SomeInteger { get; set; } …
0
votes
2 answers

Parsing a single-statement boolean expression tree

MSDN documentation has a nice example of parsing an expression tree: // Create an expression tree. Expression> exprTree = num => num < 5; // Decompose the expression tree. ParameterExpression param =…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
0
votes
1 answer

How to write left outer join using MethodCallExpressions?

The code block below answers the question: "How do you perform a left outer join using linq extension methods?" var qry = Foo.GroupJoin( Bar, foo => foo.Foo_Id, bar => bar.Foo_Id, (x,y) => new { Foo = x, Bars = y…
0
votes
1 answer

In join how to segregate the select statement

In my project one table is connected with two or more tables ,To require wanted out put need to join them,Join is not my problem ,after join want to select desired column but from a segregate expression , as like bellow syntax: public IEnumerable…
shamim
  • 6,640
  • 20
  • 85
  • 151
0
votes
1 answer

Decoupling Entity Framework from my POCO classes

I'm dynamically creating my DbContext by iterating over any entities that inherit from EntityBase and adding them to my Context: private void AddEntities(DbModelBuilder modelBuilder) { var entityMethod =…
powlette
  • 1,800
  • 20
  • 41
0
votes
2 answers

Complicated Linq expression builder

I need to build a configurable query builder that allows the user to compound any number of possible conditions. For example, lets say we have the classic case of Customer, Order, OrderItem and Product. The user wants to be able to draw a report…
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
0
votes
1 answer

Dynamic expression parsing in localized environment

So I'm trying to parse a simple arithmetic dynamic expression using System.Linq.Dynamic. This runs fine when executed in an English environment where the CurrentCulture is English-US (and the decimal separator is a plain "." dot). Trying to run the…
alrotem
  • 92
  • 1
  • 7
0
votes
1 answer

NullReferenceException while evaluating dynamically built expression tree

I'm building this expression tree: .Lambda #Lambda1 (RTX.Ulysses.TestFramework.TestCaseDataResult $x) { .Block( …
Archeg
  • 8,364
  • 7
  • 43
  • 90
0
votes
2 answers

HtmlHelper generic reflection - get metadata for non-model based expression

I have a model-based DescriptionFor helper that looks like this: public static HtmlString DescriptionFor( this HtmlHelper htmlHelper, Expression> expression) where TModel : class { var…
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254