In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the methods of LINQ dynamic query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In LINQ dynamic queries, Lambda expressions are the basis of many standard query operators, and the compiler creates lambda expressions to capture calculations defined in 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 Lambda expressions assigned to variables of type Expression. It can also be used to create LINQ dynamic 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 lambda expression.
The following examples describe how to use the expression directory tree to create LINQ dynamic queries.
Select of 1.LINQ dynamic query the following example shows how to use the expression tree to construct a dynamic query based on the IQueryable data source, query the ContactName of each customer, and use the GetCommand method to get it to generate the SQL statement.
/ / 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.ContactNameExpression 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 the GetCommand method to get the 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]
The following example of 2.LINQ dynamic query Where is to "build" the Where usage to dynamically query the city's customers in London.
IQueryable custs = db.Customers; / / create a parameter cParameterExpression param = Expression.Parameter (typeof (Customer), "c"); c.Cityboat = "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.Cityfloor = "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] = @ p0muri-@ p0: Input NVarChar (Size = 6) Prec = 0; Scale = 0) [London]
OrderBy of 3.LINQ dynamic query this example implements both sorting and filtering functions.
IQueryable custs = db.Customers; / / create a parameter cParameterExpression param = Expression.Parameter (typeof (Customer), "c"); c.Cityboat = "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.Citybirds = "London") MethodCallExpression whereCallExpression = Expression.Call (typeof (Queryable), "Where", new Type [] {typeof (Customer)}, Expression.Constant (custs), pred)) OrderBy (ContactName = > ContactName) MethodCallExpression orderByCallExpression = Expression.Call (typeof (Queryable), "OrderBy", new Type [] {typeof (Customer), typeof (string)}, whereCallExpression, Expression.Lambda (Expression.Property (param, "ContactName"), param)); / / generate dynamic query IQueryable query = db.Customers.AsQueryable (). Provider.CreateQuery (orderByCallExpression)
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] = @ p0ORDER BY [t0]. [ContactName]-- @ p0: Input NVarChar (Size = 6) Prec = 0; Scale = 0) [London]
The following example of 4.LINQ dynamic query Union uses an expression tree to dynamically query cities where customers and employees are present.
/ / e.CityIQueryable custs = db.Customers; ParameterExpression param1 = Expression.Parameter (typeof (Customer), "e"); Expression left1 = Expression.Property (param1,typeof (Customer) .GetProperty ("City")); Expression pred1 = Expression.Lambda (left1, param1); c.CityIQueryable employees = db.Employees;ParameterExpression param2 = Expression.Parameter (typeof (Employee), "c"); Expression left2 = Expression.Property (param2, typeof (Employee) .GetProperty ("City")) Expression pred2 = Expression.Lambda (left2, param2); Select (e = > e.City) Expression expr1 = Expression.Call (typeof (Queryable), "Select", new Type [] {typeof (Customer), typeof (string)}, Expression.Constant (custs), pred1); Select (c = > c.City) Expression expr2 = Expression.Call (typeof (Queryable), "Select", new Type [] {typeof (Employee), typeof (string)}, Expression.Constant (employees), pred2) / / generate dynamic query IQueryable Q1 = db.Customers.AsQueryable () .Provider.CreateQuery (expr1); IQueryable Q2 = db.Employees.AsQueryable () .Provider.CreateQuery (expr2); / / Union var Q3 = q1.Union (Q2)
The generated SQL statement is:
SELECT [T2]. [City] FROM (SELECT [t0]. [City] FROM [dbo]. [Customers] AS [t0] UNION SELECT [T1]. [City] FROM [dbo]. [Employees] AS [T1] AS [T2] what are the methods of LINQ dynamic query? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.