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

Create Expression for new Action where T is unknown at compile time

Edit2: removed a load of gubbins + bounty. I have deconstructed an expression for a message bus in the hope of reconstructing it and invoking it in a slightly different manner. The serialization and deserialization is successful and I am able to…
Peter Lea
  • 1,731
  • 2
  • 15
  • 24
0
votes
1 answer

Determine Object from Object.Property (used in persisting settings)

I have made a simple Class to save user settings. All is working well but I'd like to simplify/generalize a little more, but I am having trouble getting the Object (TestSavedSettings) from it's Property (FormLocation). As seen in the code below I…
curtis1757
  • 43
  • 4
0
votes
1 answer

Method 'Boolean Equals(System.DateTime)' is not defined for type 'System.Nullable`1[System.DateTime]'

i m working with column sorting. here is my code that returns IQueryable result. here i added one Helper class to App_Code side and created one static IQueryable method for column sorting. public static class Helper { public static IQueryable
shalin gajjar
  • 646
  • 4
  • 15
  • 44
0
votes
1 answer

Nullable Expressions

I have this method public Sorting(Expression> property, bool ascending = true) and I want to be able to call it something like this public Sorting(null) By this I mean I want to specify no expression and use the default value for…
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
0
votes
1 answer

Where is the DLR Expression Tree API?

Linq Expression Trees can be found in the System.Linq.Expressions namespace in the System.Core.dll assembly. In which assembly and in what namespace is the DLR Expression Tree API? Is it that the DLR adds more language elements as more expression…
0
votes
1 answer

Createing Expression, IOrderedQueryable>>?

I wanna create an Expression, IOrderedQueryable>>, I have the following codes : Expression selector = q => q.RegistrationDate MethodInfo orderByMethodInfo = typeof(Queryable).GetMethods().First(method => method.Name ==…
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
0
votes
1 answer

Linq Expression Decimal Contains method

I have a class that essentially converts a grids filter into a dynamic linq expression. The grid contains a simple search that takes a single input from user and I would like to create a contains statement that can be used as a generic search. …
afreeland
  • 3,889
  • 3
  • 31
  • 42
0
votes
1 answer

Why does this expression not give me the expected result?

Expression> expKeyword = x => x.InvoiceNo.StartsWith(txtSearch.Text) | x.Alias.StartsWith(txtSearch.Text); Expression> expFromDate = x => x.Date > dtpFrom.DateTime.Date; Expression
Kas
  • 3,747
  • 5
  • 29
  • 56
0
votes
1 answer

Emebeded Statement can not be a declaration or labled stateme

I am trying to store a linq express in a variable, But it gives me this compile time error Emebeded Statement can not be a declaration or labled stateme I found how to assign a linq expression to a variable from this question of…
Kas
  • 3,747
  • 5
  • 29
  • 56
0
votes
1 answer

Is there any way to check if ConstantExpression containing number is negative?

I have the following expression int someNumber = somevalue; ConstantExpression expr = Expression.Constant(someNumber); Is there a way to check to see if expr < 0 or expr is a negative number ?
fahadash
  • 3,133
  • 1
  • 30
  • 59
0
votes
3 answers

How to get value from string expression

I have a string stored in my db: "Users.ElementAt(1).LastName" I then have an object like so: MyClass myclass = new MyClass () { Users = new List() { new User() { …
Steve Stokes
  • 1,200
  • 18
  • 36
0
votes
1 answer

Expression failure

I've got such expression: Linq2Rest.Reactive.InnerRestObservable`1[A] .Where(item => (Convert(IIF((item != null), item.ID, 0)) == Convert(61))) .Skip(0) .Take(20) When I invoke Subscribe method on it I recieve such error: variable…
Pavel.S.
  • 33
  • 5
0
votes
1 answer

LinqToSQL generic lookup of foreign key entity to string

I am working with Linq To Sql and am trying to keep a history of changes and store these database. I know there are frameworks for this out there such as DoddleAudit but it feels too buggy and bloaty to me so I'm trying to create my own. This is…
Peter
  • 14,221
  • 15
  • 70
  • 110
0
votes
1 answer

Linq "Full Text" Search

I'm using this search function. but I need it to do an "and" not an "or" I cant seem to get it to return the results I want. I need to perform a search function where the search results match the text entered in box. But only a partial search. For…