av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

LINQ動態(tài)查詢的全面分析

LINQ動態(tài)查詢不是很容易就實現(xiàn)的,但是一旦能夠熟練運用了,那LINQ動態(tài)查詢能起很大作用,本文筆者就來向你介紹一下LINQ動態(tài)查詢。

成都創(chuàng)新互聯(lián)公司-專業(yè)網站定制、快速模板網站建設、高性價比岷縣網站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式岷縣網站制作公司更省心,省錢,快速模板網站建設找我們,業(yè)務覆蓋岷縣地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。

本文介紹LINQ的高級特性,其包括大家都關心的LINQ動態(tài)查詢的用法,另外簡單提下ID標識這個知識。

LINQ動態(tài)查詢有這樣一個場景:應用程序可能會提供一個用戶界面,用戶可以使用該用戶界面指定一個或多個謂詞來篩選數(shù)據(jù)。這種情況在編譯時不知道查詢的細節(jié),LINQ動態(tài)查詢將十分有用。

在LINQ中,Lambda表達式是許多標準查詢運算符的基礎,編譯器創(chuàng)建lambda表達式以捕獲基礎查詢方法(例如 Where、Select、Order By、Take While 以及其他方法)中定義的計算。表達式目錄樹用于針對數(shù)據(jù)源的結構化查詢,這些數(shù)據(jù)源實現(xiàn)IQueryable 。

例如,LINQ to SQL 提供程序實現(xiàn) IQueryable 接口,用于查詢關系數(shù)據(jù)存儲。C#和Visual Basic編譯器會針對此類數(shù)據(jù)源的查詢編譯為代碼,該代碼在運行時將生成一個表達式目錄樹。然后,查詢提供程序可以遍歷表達式目錄樹數(shù)據(jù)結構,并將其轉換為適合于數(shù)據(jù)源的查詢語言。

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

System.Linq.Expressions命名空間提供用于手動生成表達式目錄樹的API。Expression類包含創(chuàng)建特定類型的表達式目錄樹節(jié)點的靜態(tài)工廠方法,例如,ParameterExpression(表示一個已命名的參數(shù)表達式)或 MethodCallExpression(表示一個方法調用)。

編譯器生成的表達式目錄樹的根始終在類型Expression 的節(jié)點中,其中TDelegate是包含至多五個輸入參數(shù)的任何TDelegate委托;也就是說,其根節(jié)點是表示一個lambda表達式。

下面幾個例子描述如何使用表達式目錄樹來創(chuàng)建動態(tài)LINQ查詢。

1.LINQ動態(tài)查詢之Select

下面例子說明如何使用表達式樹依據(jù) IQueryable 數(shù)據(jù)源構造一個動態(tài)查詢,查詢出每個顧客的ContactName,并用GetCommand方法獲取其生成SQL語句。

 
 
 
  1. //依據(jù)IQueryable數(shù)據(jù)源構造一個查詢  
  2. IQueryable custs = db.Customers;  
  3. //組建一個表達式樹來創(chuàng)建一個參數(shù)  
  4. ParameterExpression param =   
  5.     Expression.Parameter(typeof(Customer), "c");  
  6. //組建表達式樹:c.ContactName  
  7. Expression selector = Expression.Property(param,  
  8.     typeof(Customer).GetProperty("ContactName"));  
  9. Expression pred = Expression.Lambda(selector, param);  
  10. //組建表達式樹:Select(c=>c.ContactName)  
  11. Expression expr = Expression.Call(typeof(Queryable), "Select",  
  12.     new Type[] { typeof(Customer), typeof(string) },  
  13.     Expression.Constant(custs), pred);  
  14. //使用表達式樹來生成動態(tài)查詢  
  15. IQueryable query = db.Customers.AsQueryable()  
  16.     .Provider.CreateQuery(expr);  
  17. //使用GetCommand方法獲取SQL語句  
  18. System.Data.Common.DbCommand cmd = db.GetCommand(query);  
  19. 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ù)c  
  3. ParameterExpression param =   
  4.     Expression.Parameter(typeof(Customer), "c");  
  5. //c.City=="London"  
  6. Expression left = Expression.Property(param,  
  7.     typeof(Customer).GetProperty("City"));  
  8. Expression right = Expression.Constant("London");  
  9. Expression filter = Expression.Equal(left, right);  
  10. Expression pred = Expression.Lambda(filter, param);  
  11. //Where(c=>c.City=="London")  
  12. Expression expr = Expression.Call(typeof(Queryable), "Where",  
  13.     new Type[] { typeof(Customer) },   
  14.     Expression.Constant(custs), pred);  
  15. //生成動態(tài)查詢  
  16. IQueryable query = db.Customers.AsQueryable()  
  17.     .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], [t0].[Fax]  
  4. FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] = @p0  
  5. -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]3.OrderBy 

本例既實現(xiàn)排序功能又實現(xiàn)了過濾功能。

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

生成的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], [t0].[Fax]  
  4. FROM [dbo].[Customers] AS [t0] WHERE [t0].[City] = @p0  
  5. ORDER BY [t0].[ContactName]  
  6. -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]4.Union  

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

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

生成的SQL語句為:

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

在前面這一點沒有說到,在這里作為高級特性單獨說下ID標識。

這個例子說明我們存儲一條新的記錄時候,ContactID作為主鍵標識,系統(tǒng)自動分配,標識種子為1,所以每次自動加一。

 
 
 
  1. //ContactID是主鍵ID,插入一條數(shù)據(jù),系統(tǒng)自動分配ID  
  2. Contact con = new Contact()  
  3. {  
  4.     CompanyName = "New Era",  
  5.     Phone = "(123)-456-7890" 
  6. };  
  7. db.Contacts.InsertOnSubmit(con);  
  8. db.SubmitChanges();  

以上就是對LINQ動態(tài)查詢的詳細闡述。

【編輯推薦】

  1. 深入淺出 LINQ表達式
  2. LINQ基礎學習之LINQ to XML
  3. 學習心得LINQ to XML
  4. 淺析LINQ開發(fā)技術之LINQ to XML
  5. 詳細闡述linq動態(tài)排序

新聞名稱:LINQ動態(tài)查詢的全面分析
鏈接地址:http://uogjgqi.cn/article/dhceehd.html
掃二維碼與項目經理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網交流