自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深度講解LINQ動態(tài)查詢

開發(fā) 后端
表達式目錄樹在LINQ中用于表示分配給類型為Expression<TDelegate>的變量的Lambda表達式。還可用于創(chuàng)建LINQ動態(tài)查詢。

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

在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語句。

  1. //依據(jù)IQueryable數(shù)據(jù)源構(gòu)造一個查詢  
  2. IQueryable custs = db.Customers;  
  3. //組建一個表達式樹來創(chuàng)建一個參數(shù)  
  4. ParameterExpression param =     Expression.Parameter(typeof(Customer), "c");  
  5. //組建表達式樹:  
  6. c.ContactNameExpression selector = Expression.Property(param, 
  7.    typeof(Customer).GetProperty("ContactName"));
  8. Expression pred = Expression.Lambda(selector, param);  
  9. //組建表達式樹:  
  10. Select(c=>c.ContactName)Expression expr = 
  11. Expression.Call(typeof(Queryable), "Select",   
  12.  new Type[] { typeof(Customer), typeof(string) },    
  13. Expression.Constant(custs), pred);  
  14. //使用表達式樹來生成動態(tài)查詢  
  15. IQueryable<string> query = 
  16. db.Customers.AsQueryable()    .Provider.CreateQuery<string>(expr);  
  17. //使用GetCommand方法獲取SQL語句  
  18. System.Data.Common.DbCommand cmd = 
  19. db.GetCommand(query);Console.WriteLine(cmd.CommandText); 

生成的SQL語句為:

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

2.LINQ動態(tài)查詢之Where下面一個例子是“搭建”Where用法來動態(tài)查詢城市在倫敦的顧客。

  1. IQueryable custs = db.Customers;  
  2. //創(chuàng)建一個參數(shù)  
  3. cParameterExpression param =     
  4. Expression.Parameter(typeof(Customer), "c");  
  5. c.City=="London"Expression left = Expression.Property(param,    
  6. typeof(Customer).GetProperty("City"));
  7. Expression right = Expression.Constant("London");
  8. Expression filter = Expression.Equal(left, right);
  9. Expression pred = Expression.Lambda(filter, param);  
  10. Where(c=>c.City=="London")Expression expr = 
  11. Expression.Call(typeof(Queryable), 
  12. "Where",    new Type[] { typeof(Customer) },     
  13. Expression.Constant(custs), pred);  
  14. //生成動態(tài)查詢IQueryable query =
  15.  db.Customers.AsQueryable()    .Provider.CreateQuery(expr); 

生成的SQL語句為:

  1. SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],  
  2.  [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region],   
  3. [t0].[PostalCode], [t0].[Country], [t0].[Phone],   
  4. [t0].[Fax]FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] = 
  5. @p0-- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]  

3.LINQ動態(tài)查詢之OrderBy本例既實現(xiàn)排序功能又實現(xiàn)了過濾功能。

  1. IQueryable custs = db.Customers;    
  2. //創(chuàng)建一個參數(shù)cParameterExpression param =     
  3. Expression.Parameter(typeof(Customer), "c");    
  4. c.City=="London"Expression left = Expression.Property(param,     
  5.  typeof(Customer).GetProperty("City"));Expression right =   
  6. Expression.Constant("London");    
  7. Expression filter = Expression.Equal(left, right);Expression pred =  
  8.  Expression.Lambda(filter, param);    
  9. Where(c=>c.City=="London")MethodCallExpression whereCallExpression =  
  10.  Expression.Call(    typeof(Queryable), "Where",     
  11.  new Type[] { typeof(Customer) },    Expression.Constant(custs), pred);    
  12. OrderBy(ContactName =>   
  13. ContactName)MethodCallExpression orderByCallExpression =  
  14.  Expression.Call(    typeof(Queryable), "OrderBy",      
  15. new Type[] { typeof(Customer), typeof(string) },      
  16.  whereCallExpression,     
  17.  Expression.Lambda(Expression.Property    (param, "ContactName"), param));    
  18. //生成動態(tài)查詢    
  19. IQueryable query =  
  20. db.Customers.AsQueryable().Provider.CreateQuery
  21. (orderByCallExpression);   

生成的SQL語句為:

  1. SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],   
  2. [t0].[ContactTitle], [t0].[Address], [t0].[City], 
  3. [t0].[Region],[t0].[PostalCode],   
  4. [t0].[Country], [t0].[Phone],  
  5.  [t0].[Fax]FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] = 
  6. @p0ORDER BY [t0].[ContactName]-- @p0:
  7.  Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London] 

4.LINQ動態(tài)查詢之Union下面的例子使用表達式樹動態(tài)查詢顧客和雇員同在的城市。

  1. //e.CityIQueryable custs = db.Customers;          
  2. ParameterExpression param1 = Expression.Parameter(typeof(Customer), "e");  
  3. Expression left1 = Expression.Property(param1,
  4. typeof(Customer).GetProperty("City"));
  5. Expression pred1 = Expression.Lambda(left1, param1);  
  6. c.CityIQueryable employees = 
  7. db.Employees;ParameterExpression param2 =
  8.  Expression.Parameter(typeof(Employee), "c");
  9. Expression left2 = Expression.Property(param2,   
  10.   typeof(Employee).GetProperty("City"));
  11. Expression pred2 = Expression.Lambda(left2, param2);  
  12. Select(e=>e.City)Expression expr1 =
  13.  Expression.Call(typeof(Queryable), "Select",    
  14.  new Type[] { typeof(Customer), typeof(string) },
  15. Expression.Constant(custs), pred1);  
  16. Select(c=>c.City)Expression expr2 =
  17.  Expression.Call(typeof(Queryable), "Select",    
  18.  new Type[] { typeof(Employee), typeof(string) }, 
  19. Expression.Constant(employees), pred2);  
  20. //生成動態(tài)查詢  
  21. IQueryable<string> q1 = 
  22. db.Customers.AsQueryable().Provider.CreateQuery<string>(expr1);
  23. IQueryable<string> q2 = 
  24. db.Employees.AsQueryable().Provider.CreateQuery<string>(expr2);  
  25. //并集  
  26. var q3 = q1.Union(q2);  
  27.  

生成的SQL語句為:

  1. SELECT [t2].[City]  
  2. FROM   
  3. (    SELECT [t0].[City] FROM [dbo].[Customers] AS [t0]    
  4. UNION    SELECT [t1].[City] FROM [dbo].[Employees] AS [t1]    )   
  5. AS [t2]  

以上就是關(guān)于LINQ動態(tài)查詢的一些方法。

【編輯推薦】

  1. LINQ動態(tài)查詢的實現(xiàn)淺析
  2. LINQ TO SQL動態(tài)修改表名稱的實現(xiàn)淺析
  3. LINQ To SQL的一點討論
  4. 淺析LINQ事務(wù)處理的實現(xiàn)
  5. 淺析DataSet和DataTable
責任編輯:阡陌 來源: 邀云AA網(wǎng)
相關(guān)推薦

2009-09-15 09:45:23

Linq動態(tài)條件

2009-09-17 16:46:34

Linq to sql

2009-09-18 15:15:12

LINQ to SQL

2009-09-15 09:19:22

linq動態(tài)條件

2009-09-17 17:03:13

LINQ動態(tài)查詢

2009-09-14 19:14:51

LINQ動態(tài)查詢

2009-09-17 16:20:43

Linq to sql

2009-09-17 09:11:26

LINQ查詢

2009-09-17 14:21:19

LINQ表達式

2009-09-17 13:10:48

linq動態(tài)排序

2009-09-15 14:52:15

linq級聯(lián)刪除

2009-09-17 13:15:20

LINQ查詢

2009-09-16 10:38:43

LINQ查詢

2009-09-15 10:46:04

LINQ to SQL

2009-09-16 10:08:06

LINQ查詢

2009-09-09 16:53:53

LINQ查詢語法

2009-09-14 10:13:02

LINQ查詢操作

2009-09-10 16:28:17

LINQ查詢

2009-09-14 10:09:26

LINQ查詢結(jié)果

2009-09-08 17:27:18

LINQ to Dat
點贊
收藏

51CTO技術(shù)棧公眾號