新聞中心
LINQ動態(tài)查詢運用的人很少,也許因為排斥,也許因為難以實現(xiàn),本文筆者就為大家介紹幾種LINQ動態(tài)查詢方法。

創(chuàng)新互聯(lián)公司專注于隆子企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都做商城網(wǎng)站。隆子網(wǎng)站建設(shè)公司,為隆子等地區(qū)提供建站服務(wù)。全流程按需開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
在LINQ動態(tài)查詢中,Lambda表達式是許多標準查詢運算符的基礎(chǔ),編譯器創(chuàng)建lambda表達式以捕獲基礎(chǔ)查詢方法(例如 Where、Select、Order By、Take While 以及其他方法)中定義的計算。表達式目錄樹用于針對數(shù)據(jù)源的結(jié)構(gòu)化查詢,這些數(shù)據(jù)源實現(xiàn)IQueryable 。
例如,LINQ to SQL 提供程序?qū)崿F(xiàn) IQueryable 接口,用于查詢關(guān)系數(shù)據(jù)存儲。C#和Visual Basic編譯器會針對此類數(shù)據(jù)源的查詢編譯為代碼,該代碼在運行時將生成一個表達式目錄樹。然后,查詢提供程序可以遍歷表達式目錄樹數(shù)據(jù)結(jié)構(gòu),并將其轉(zhuǎn)換為適合于數(shù)據(jù)源的查詢語言。
表達式目錄樹在LINQ中用于表示分配給類型為Expression 的變量的Lambda表達式。還可用于創(chuàng)建LINQ動態(tài)查詢。
System.Linq.Expressions命名空間提供用于手動生成表達式目錄樹的API。Expression類包含創(chuàng)建特定類型的表達式目錄樹節(jié)點的靜態(tài)工廠方法,例如,ParameterExpression(表示一個已命名的參數(shù)表達式)或 MethodCallExpression(表示一個方法調(diào)用)。編譯器生成的表達式目錄樹的根始終在類型Expression 的節(jié)點中,其中TDelegate是包含至多五個輸入?yún)?shù)的任何TDelegate委托;也就是說,其根節(jié)點是表示一個lambda表達式。
下面幾個例子描述如何使用表達式目錄樹來創(chuàng)建LINQ動態(tài)查詢。
1.LINQ動態(tài)查詢之Select下面例子說明如何使用表達式樹依據(jù) IQueryable 數(shù)據(jù)源構(gòu)造一個動態(tài)查詢,查詢出每個顧客的ContactName,并用GetCommand方法獲取其生成SQL語句。
- //依據(jù)IQueryable數(shù)據(jù)源構(gòu)造一個查詢
- IQueryable custs = db.Customers;
- //組建一個表達式樹來創(chuàng)建一個參數(shù)
- ParameterExpression param = Expression.Parameter(typeof(Customer), "c");
- //組建表達式樹:
- c.ContactNameExpression selector = Expression.Property(param,
- typeof(Customer).GetProperty("ContactName"));
- Expression pred = Expression.Lambda(selector, param);
- //組建表達式樹:
- Select(c=>c.ContactName)Expression expr =
- Expression.Call(typeof(Queryable), "Select",
- new Type[] { typeof(Customer), typeof(string) },
- Expression.Constant(custs), pred);
- //使用表達式樹來生成動態(tài)查詢
- IQueryable
query = - db.Customers.AsQueryable() .Provider.CreateQuery
(expr); - //使用GetCommand方法獲取SQL語句
- System.Data.Common.DbCommand cmd =
- db.GetCommand(query);Console.WriteLine(cmd.CommandText);
生成的SQL語句為:
- SELECT [t0].[ContactName] FROM [dbo].[Customers] AS [t0]
2.LINQ動態(tài)查詢之Where下面一個例子是“搭建”Where用法來動態(tài)查詢城市在倫敦的顧客。
- IQueryable custs = db.Customers;
- //創(chuàng)建一個參數(shù)
- cParameterExpression param =
- Expression.Parameter(typeof(Customer), "c");
- c.City=="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.City=="London")Expression expr =
- Expression.Call(typeof(Queryable),
- "Where", new Type[] { typeof(Customer) },
- Expression.Constant(custs), pred);
- //生成動態(tài)查詢IQueryable query =
- db.Customers.AsQueryable() .Provider.CreateQuery (expr);
生成的SQL語句為:
- 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]
3.LINQ動態(tài)查詢之OrderBy本例既實現(xiàn)排序功能又實現(xiàn)了過濾功能。
- IQueryable custs = db.Customers;
- //創(chuàng)建一個參數(shù)cParameterExpression param =
- Expression.Parameter(typeof(Customer), "c");
- c.City=="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.City=="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));
- //生成動態(tài)查詢
- IQueryable query =
- db.Customers.AsQueryable().Provider.CreateQuery
- (orderByCallExpression);
生成的SQL語句為:
- 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]
4.LINQ動態(tài)查詢之Union下面的例子使用表達式樹動態(tài)查詢顧客和雇員同在的城市。
- //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);
- //生成動態(tài)查詢
- IQueryable
q1 = - db.Customers.AsQueryable().Provider.CreateQuery
(expr1); - IQueryable
q2 = - db.Employees.AsQueryable().Provider.CreateQuery
(expr2); - //并集
- var q3 = q1.Union(q2);
生成的SQL語句為:
- SELECT [t2].[City]
- FROM
- ( SELECT [t0].[City] FROM [dbo].[Customers] AS [t0]
- UNION SELECT [t1].[City] FROM [dbo].[Employees] AS [t1] )
- AS [t2]
以上就是關(guān)于LINQ動態(tài)查詢的一些方法。
【編輯推薦】
- LINQ動態(tài)查詢的實現(xiàn)淺析
- LINQ TO SQL動態(tài)修改表名稱的實現(xiàn)淺析
- LINQ To SQL的一點討論
- 淺析LINQ事務(wù)處理的實現(xiàn)
- 淺析DataSet和DataTable
文章名稱:深度講解LINQ動態(tài)查詢
文章位置:http://m.fisionsoft.com.cn/article/codjeoe.html


咨詢
建站咨詢
