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
1 answer

Tweaking the SQL Telerik OpenAccess generates from LINQ expression

I use the specification pattern to dynamically generate LINQ expressions, which will be used On entities in memory For SQL code generation by OpenAccess See this blog post for an example of what I'm trying to achieve. It all works fine so far,…
EagleBeak
  • 6,939
  • 8
  • 31
  • 47
0
votes
1 answer

How to add Select to an existing Linq Expression

I'm trying to make an editable table. I have a dynamic table, columns can be made visible or invisible. I have an extra column with an edit button. When I hit this I do an Ajax call to get an editable version of the row. My viewmodel: public class…
Kasper Cottaar
  • 462
  • 3
  • 14
0
votes
1 answer

String Comparisons using LINQ Expressions and String.Format

I'm using LINQ expressions to dynamically search for values in a collection and I've come across a strange issue that seems to be caused by searching for a string that is the result of a String.Format() operation. Here is a simplified version of…
mclark1129
  • 7,532
  • 5
  • 48
  • 84
0
votes
2 answers

How to create a lambda expression from a type object with LINQ?

I'm trying to speed up reflection -> SetValue with a LINQ expression. My problem is this method: public void SetValue(T obj) { FieldInfo field = typeof(T).GetField("Title", BindingFlags.Instance | …
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0
votes
1 answer

Struggling with Lambda expression (VB .net)

I have a relatively simple thing that I can do easily in SQL, but I'm trying to get used to using Lambda expressions, and having a hard time. Here is a simple example. Basically I have 2 tables. tblAction (ActionID, ActionName) tblAudit…
-1
votes
1 answer

Append Filtered Include to Expression

In our project we have texts for multiple languages stored in our database. I want to create a helper function that includes a text in a query. This would be useful because this include happens a lot in the application and I want have the include…
Luuk
  • 3
  • 3
-1
votes
3 answers

C# How to pass a variable into a func?

Simple version var myVar = some object; myList.Where(element => myVar != null && element.Name == myVar.Name) What I want to write public static (Return Value) Start(this T input, Func<(IDK what goes here)> exp) { var theVar = somehow get the…
-1
votes
1 answer

How to construct IEnumerable from a list of lambda expression in c#?

In a C# project, I want to be able to create a function that accepts an IQuereable object along with multiple lambda's expressions then converts it into a different object. However, instead of pulling all the available properties from the…
Junior
  • 11,602
  • 27
  • 106
  • 212
-1
votes
1 answer

c# How to evaluate compareTo(..) in Expression which returns a string

I have similar Situation like here: How to declare a Linq Expression variable in order to have it processed as a dbParameter But in my case, don't want to compare with 'equal' operator, I need to compare one string against another. How can I achieve…
user1470240
  • 580
  • 3
  • 19
-1
votes
1 answer

declaring local variables in c# expression trees bound to a block

My problem here is that I cannot get local variables to blocks in c# expression trees (e.g. System.Linq.Expressions) to work without throwing an exception despite function argument variables working perfectly fine. By 'local variables' here, I…
Nick
  • 920
  • 1
  • 7
  • 21
-1
votes
2 answers

how to set value in List with out using foreach loop

I am having two lists and filter based on a group of values. var UserIdList = response.Users.Select(p => p.Id).ToList(); var filteredRecords = (from o in om.Organizations join u in om.Users on o.Id equals u.OrganizationId where…
AMDI
  • 895
  • 2
  • 17
  • 40
-1
votes
1 answer

De/Serialize basic Linq Expressions using BinarySerializer

I'm looking for a library/tool in order to be able to de/serialize Linq Expressions. Is there some library over there? Is already suported natively on .Net? How could I get an starter stuff code?
Jordi
  • 20,868
  • 39
  • 149
  • 333
-1
votes
2 answers

Is it possible to simplify this LINQ expression for Entity Framework?

I'm implementing a basic search service, and several different repositories are injected into it. The repositories have a method that allows for an expression to be used like so: public IEnumerable Select(Expression> predicate) …
user9993
  • 5,833
  • 11
  • 56
  • 117
-1
votes
1 answer

Error in Create Dynamic Linq Query Expression

PropertyInfo[] All_prop = Get_All_prop2(Model); //------Get Properties has Value----- foreach (PropertyInfo property in All_prop) { //--Check Has Value Property if (property.GetValue(this, null) != null) { …
-1
votes
3 answers

How can I call a reflected Func property using Expression Trees

I have a generic class with a lambda property defined as such: public class Transformation : TransformationBase { public Func Transform { get; private set; } ... I'm trying to compile an Action that can…
Dejan
  • 9,150
  • 8
  • 69
  • 117
1 2 3
39
40