Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression.Lambda and query generation at runtime, simplest "Where" example

I was trying to generate a simple Lambda Expression at runtime with no luck... something like this:

var result = queryableData.Where(item => item.Name == "Soap") 

Here is my example class and a fixture queryable:

public class Item {     public int Id { get; set; }     public string Name { get; set; } }  IQueryable<Item> queryableData = ...; 

Then I generate a lambda expression at runtime correct code follows:

//"item" in "item =>..." var item = Expression     .Parameter(typeof(Item), "item");  //property of my item, this is "item.Name" var prop = Expression     .Property(item, "Name");  //then "Soap" in '... => item.Name=="Soap"' var value = Expression.Constant("Soap");  //equality expression "==" in my primer var equals = Expression.Equal(prop, value);  //then lambda var lambda = Expression.Lambda<Func<Item, bool>>(equals, item);  //and here are the results     var results = queryableData.Where(lambda); 

Big thanks to dtb for advice!

like image 474
CodeAddicted Avatar asked Nov 29 '11 18:11

CodeAddicted


People also ask

What is lambda function in C#?

A lambda expression with an expression on the right side of the => operator is called an expression lambda. An expression lambda returns the result of the expression and takes the following basic form: C# Copy. (input-parameters) => expression. The body of an expression lambda can consist of a method call.

What is expression tree in LINQ?

You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to use expression trees to build dynamic queries (C#).

Can we use dynamic in lambda expressions?

In 2010, the Dynamic Type was introduced and that gave us the ability to create dynamic lambda expressions.

What are lambda expressions?

Here you learn how to write Lambda expressions in C# , Lambda expressions are type of anonymous functions that contain sequence of operators, which are termed as expressions Why Lambda?

How do you select one column in a lambda expression?

lambda select one column c# Lambda expression select example. // Way 1 : (Query syntax) var studentNames = from s in studentsStream select s.FullName; //Way 2: Using Lambda Expression var _studentNames = studentsStream.Select<Student, string> (s => s.FullName);

How to use expression builder in Linux?

To use expression builder you need to use namespace reference System.Linq.Expressions;

What is the expected behavior of a LINQ query?

The expected behavior is that it returns the elements from source that satisfy the condition specified by predicate. The index of each source element is provided as the second argument to predicate. Learn how to perform grouped joins using LINQ in C#.


1 Answers

In the following query

var result = query.Where(item => item.Name == "Soap") 

the lambda expression is

item => item.Name == "Soap" 

You only need to construct this part, not the Where call which accepts an expression tree.

The expression tree for the lambda expression looks like this:

                     Lambda                       /  \                    Equal  Parameter                    /   \    item               Property  \                "Name"   Constant                  |       "Soap"              Parameter                         item 

In code:

var item = Expression.Parameter(typeof(Item), "item");  var prop = Expression.Property(item, "Name");  var soap = Expression.Constant("Soap");  var equal = Expression.Equal(prop, soap);  var lambda = Expression.Lambda<Func<Item, bool>>(equal, item);  var result = queryableData.Where(lambda); 
like image 70
dtb Avatar answered Oct 21 '22 13:10

dtb