Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Lambda expression in LINQ

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the Lambda expression in LINQ, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Linq has a lot to learn, and here we focus on LINQ Lambda expressions, including the LINQ Lambda expressions used in the expression directory tree to represent variables of type Expression in LINQ.

There is a scenario where an application may provide a user interface that users can use to specify one or more predicates to filter data. In this case, you don't know the details of the query at compile time, and dynamic queries can be very useful.

LINQ Lambda expressions are the basis of many standard query operators, and the compiler creates lambda expressions to capture the calculations defined in the underlying query methods, such as Where, Select, Order By, Take While, and others. The expression directory tree is used for structured queries against data sources that implement IQueryable. For example, the LINQ to SQL provider implements the IQueryable interface, which is used to query relational data stores. The C # and Visual Basic compilers compile queries for such data sources into code that generates an expression directory tree at run time. The query provider can then traverse the expression directory tree data structure and convert it into a query language suitable for the data source.

The expression directory tree is used in LINQ to represent LINQ Lambda expressions assigned to variables of type Expression. It can also be used to create dynamic LINQ queries.

The System.Linq.Expressions namespace provides API for manually generating the expression directory tree. The Expression class contains static factory methods that create a specific type of expression directory tree node, such as ParameterExpression (for a named parameter expression) or MethodCallExpression (for a method call). The root of the expression directory tree generated by the compiler is always in the node of type Expression, where TDelegate is any TDelegate delegate that contains up to five input parameters; that is, its root node represents an LINQ lambda expression.

The following examples describe how to use the expression directory tree to create dynamic LINQ queries.

1.Select

The following example shows how to use the expression tree to construct a dynamic query based on the IQueryable data source, query out the ContactName of each customer, and use the GetCommand method to get it to generate SQL statements.

/ / construct a query IQueryable custs = db.Customers; / / build an expression tree based on the IQueryable data source to create a parameter ParameterExpression param = Expression.Parameter (typeof (Customer), "c"); / / build an expression tree: c.ContactName Expression selector = Expression.Property (param, typeof (Customer) .GetProperty ("ContactName")); Expression pred = Expression.Lambda (selector, param) / / build expression tree: Select (c = > c.ContactName) Expression expr = Expression.Call (typeof (Queryable), "Select", new Type [] {typeof (Customer), typeof (string)}, Expression.Constant (custs), pred); / / use expression tree to generate dynamic query IQueryable query = db.Customers.AsQueryable () .Provider.CreateQuery (expr); / / use GetCommand method to get SQL statement System.Data.Common.DbCommand cmd = db.GetCommand (query) Console.WriteLine (cmd.CommandText)

The generated SQL statement is:

SELECT [t0]. [ContactName] FROM [dbo]. [Customers] AS [t0]

2.Where

The following example is to "build" the Where usage to dynamically query the city's customers in London.

IQueryable custs = db.Customers; / / create a parameter c ParameterExpression param = Expression.Parameter (typeof (Customer), "c"); / / c.Citycats = "London" Expression left = Expression.Property (param, typeof (Customer) .GetProperty ("City")); Expression right = Expression.Constant ("London"); Expression filter = Expression.Equal (left, right); Expression pred = Expression.Lambda (filter, param) / / Where (c = > c.Citycats = "London") Expression expr = Expression.Call (typeof (Queryable), "Where", new Type [] {typeof (Customer)}, Expression.Constant (custs), pred); / / generate dynamic query IQueryable query = db.Customers.AsQueryable () .Provider.CreateQuery (expr)

The generated SQL statement is:

SELECT [t0]. [CustomerID], [t0]. [CompanyName], [t0]. [ContactName], [t0]. [ContactTitle], [t0]. [Address], [t0]. [City], [t0]. [Region], [t0]. [PostalCode], [t0]. [Country], [t0]. [Phone] [t0]. [Fax] FROM [dbo]. [Customers] AS [t0] WHERE [t0]. [City] = @ p0-- @ p0: Input NVarChar (Size = 6) Prec = 0; Scale = 0) [London] these are all the contents of the article "how to use Lambda expressions in LINQ". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 215

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report