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

一體化查詢語言LINQ的操作詳解

數(shù)據(jù)庫 SQL Server
本文主要介紹了一體化查詢語言LINQ的一些操作,包括對(duì)字符串的查詢、數(shù)組查詢、XML查詢和數(shù)據(jù)庫表的查詢等,希望能夠?qū)δ兴鶐椭?/div>

我們知道,一體化查詢語言LINQ,即(NET Language Integrated Query),是集成在 .NET Framework 3.5 編程語言中的一種新特性,已成為編程語言的一部分,使開發(fā)人員可以使用語法基本一致的語句對(duì)不同來源不同類型的數(shù)據(jù)進(jìn)行查詢與整合,它使得查詢表達(dá)式可以得到很好的編譯時(shí)語法檢查。

一:字符串查詢

  1. string strLinq = "Hello World!";  
  2.  
  3. var result = from q in strLinq select q;  
  4.  
  5. var result1 = from q in strLinq where q == 'o' select q;  
  6.  
  7. // Linq 的擴(kuò)展方法  
  8.  
  9. var result2 = strLinq.Where(q => q == 'o');  
  10.  
  11. var result3 = Enumerable.Where(strLinq, q => q == 'o'); 

二:數(shù)組查詢

  1. string[] strs ={ "Suyama", "Fuller", "Callahan", "Michael", "Janet" };  
  2.  
  3. var result = from p in strs where p.Length > 5 select p;  
  4.  
  5. var result1 = strs.Where(p => p.Length>5);  
  6.  
  7. var result2 = strs.Where(p =>p.StartsWith("C")); 

三:XML 查詢

  1. <Employees> 
  2.  
  3. <Employee gender="0"> 
  4.  
  5. <Name>Davolio</Name> 
  6.  
  7. <Address>507 - 20th Ave. E. Apt. 2A</Address> 
  8.  
  9. </Employee> 
  10.  
  11. <Employee gender="0"> 
  12.  
  13. <Name>Andrew</Name> 
  14.  
  15. <Address>4110 Old Redmond Rd.</Address> 
  16.  
  17. </Employee> 
  18.  
  19. <Employee gender="1"> 
  20.  
  21. <Name>Laura</Name> 
  22.  
  23. <Address>Coventry House Miner Rd.</Address> 
  24.  
  25. </Employee> 
  26.  
  27. <Employee gender="1"> 
  28.  
  29. <Name>Anne</Name> 
  30.  
  31. <Address>4726 - 11th Ave. N.E.</Address> 
  32.  
  33. </Employee> 
  34.  
  35. <Employee gender="0"> 
  36.  
  37. <Name>King</Name> 
  38.  
  39. <Address>7 Houndstooth Rd.</Address> 
  40.  
  41. </Employee> 
  42.  
  43. </Employees> 
  44.  
  45. XElement root = XElement.Load("D:\\Employees.xml");  
  46.  
  47. // 查詢性別為男性(gender=1)的全部員工  
  48.  
  49. var emps = from s in root.Elements("Employee")  
  50.  
  51. where s.Attribute("gender").Value == "1"  
  52.  
  53. select s;  
  54.  
  55. // 查詢性別為女性(gender=0)的員工的姓名和住址  
  56.  
  57. var emps1 = from s in root.Elements("Employee")  
  58.  
  59. where s.Attribute("gender").Value == "0"  
  60.  
  61. select new  
  62.  
  63. {  
  64.  
  65. name = s.Element("Name").Value,  
  66.  
  67. address = s.Element("Address").Value  
  68.  
  69. }; 

#p#

四:數(shù)據(jù)庫表查詢

其中用到的表的結(jié)構(gòu)以及數(shù)據(jù)大致為下圖所示:

一體化查詢語言LINO的操作詳解

一體化查詢語言LINO的操作詳解

(1) 單表查詢

  1. // <1> 查詢所有客戶  
  2.  
  3. var customers1 = from s in dc.Customers select s;  
  4.  
  5. List<Customer> customerLst1 = customers1.ToList();  
  6.  
  7. // <2> 查詢國籍為 'Germany' 的客戶  
  8.  
  9. var customers2 = from s in dc.Customers where s.Country == "Germany" select s;  
  10.  
  11. List<Customer> customerLst2 = customers2.ToList();  
  12.  
  13. List<Customer> customerLst2_1 = (from s in dc.Customers where s.Country == "Germany" select s).ToList();  
  14.  
  15. List<Customer> customerLst2_2 = dc.Customers.Where(s => s.Country == "Germany").ToList();  
  16.  
  17. // <3> 按公司名降序排列查詢員工ID和公司  
  18.  
  19. var customerLst3 = (from s in dc.Customers  
  20.  
  21. orderby s.CompanyName descending  
  22.  
  23. select new  
  24.  
  25. {  
  26.  
  27. ID = s.CustomerID,  
  28.  
  29. Company = s.CompanyName  
  30.  
  31. }).ToList();  
  32.  
  33. // <4> 查詢公司名,并判斷其長度是不是大于20  
  34.  
  35. var customerLst4 = (from s in dc.Customers  
  36.  
  37. select new  
  38.  
  39. {  
  40.  
  41. CompanyName = s.CompanyName,  
  42.  
  43. IsThan20 = (s.CompanyName.Length > 20) ? true : false  
  44.  
  45. }).ToList();  
  46.  
  47. // <5> 按順序查詢第10到20記錄的客戶  
  48.  
  49. List<Customer> customerLst5 = (from s in dc.Customers select s).Skip(9).Take(11).ToList();  
  50.  
  51. // Skip(9): 跳過前9個(gè)  
  52.  
  53. // Take(11): 取前11條記錄  
  54.  
  55. // <6> 國籍為 Canada和USA的客戶  
  56.  
  57. var customerLst6 = from s in dc.Customers where new string[] { "Canada", "USA" }.Contains(s.Country) select s;  
  58.  
  59. // <7> 地址中有 '9'  
  60.  
  61. var customerLst7 = from s in dc.Customers where s.Address.Contains("9") select s;  
  62.  
  63. // <8> 地址以 'A' 開頭  
  64.  
  65. var customerLst8 = from s in dc.Customers where s.Address.StartsWith("A") select s;  
  66.  
  67. var customerLst8_1 = from s in dc.Customers where s.Address.IndexOf("A") == 0 select s;  
  68.  
  69. // <9> 地址以 'A' 開頭的客戶數(shù)量  
  70.  
  71. var customerLst9 = dc.Customers.Count(s => s.Address.IndexOf("A") == 0);  
  72.  
  73. // <10> 查詢最高、最低、平均、總共的付款  
  74.  
  75. var customerLst10 = dc.Customers.Select(s => s.Payment).Max();  
  76.  
  77. var customerLst10_1 = Enumerable.Select(dc.Customers, s => s.Payment).Max();  
  78.  
  79. var customerLst10_2 = dc.Customers.Select(s => s.Payment).Min();  
  80.  
  81. var customerLst10_3 = dc.Customers.Select(s => s.Payment).Average();  
  82.  
  83. var customerLst10_4 = dc.Customers.Select(s => s.Payment).Sum();  
  84.  
  85. // <11> 按國籍查詢客戶數(shù)量和最高付款  
  86.  
  87. var customerLst11 = (from s in dc.Customers  
  88.  
  89. group s by s.Country into p  
  90.  
  91. select new  
  92.  
  93. {  
  94.  
  95. Country = p.Key,  
  96.  
  97. Count = p.Count(),  
  98.  
  99. Payment = p.Max(g => g.Payment)  
  100.  
  101. }).ToList(); 

#p#

(2) 多表查詢

  1. // <1> 查詢每個(gè)客戶下訂單的日期 (內(nèi)連接)  
  2.  
  3. var customerLst1 = from s in dc.Customers  
  4.  
  5. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  6.  
  7. orderby s.CustomerID ascending  
  8.  
  9. select new  
  10.  
  11. {  
  12.  
  13. ID = s.CustomerID,  
  14.  
  15. OrderDate = p.OrderDate  
  16.  
  17. };  
  18.  
  19. // <2> 查詢每個(gè)客戶下訂單的日期 (左外連接)  
  20.  
  21. var customerLst2 = from s in dc.Customers  
  22.  
  23. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  24.  
  25. into sp from a in sp.DefaultIfEmpty()  
  26.  
  27. orderby s.CustomerID ascending  
  28.  
  29. select new  
  30.  
  31. {  
  32.  
  33. ID = s.CustomerID,  
  34.  
  35. OrderDate = a.OrderDate  
  36.  
  37. };  
  38.  
  39. // <3> 查詢每個(gè)客戶下訂單的日期,條件:付款大于200且訂單日期在1997-12-08以后 (多表?xiàng)l件查詢)  
  40.  
  41. var customerLst3 = from s in dc.Customers  
  42.  
  43. join p in dc.Orders on s.CustomerID equals p.CustomerID  
  44.  
  45. where s.Payment > 200 && p.OrderDate > DateTime.Parse("1997-12-08")  
  46.  
  47. orderby s.CustomerID ascending  
  48.  
  49. select new  
  50.  
  51. {  
  52.  
  53. ID = s.CustomerID,  
  54.  
  55. OrderDate = p.OrderDate  
  56.  
  57. }; 

實(shí)際操作起來,用linq進(jìn)行多表連接比較復(fù)雜(特別是在表很多的情況下),建議先將查詢內(nèi)容做成視圖,再把視圖映射成實(shí)體類,這樣就可以用單表查詢的方式進(jìn)行查詢了。

作者建議:LINQ在做對(duì)數(shù)據(jù)庫查詢的時(shí)候,其實(shí)就是對(duì)sql的再封裝,從而使得操作更加簡潔,而且從LINQ解析得到的sql也得到了優(yōu)化,可能針對(duì)某些查詢,比沒有優(yōu)化的sql查詢效率更高些,所以,如果開發(fā)人員追求效率(對(duì)數(shù)據(jù)的增、刪、改、查),建議先做存儲(chǔ)過程,然后優(yōu)化,再把這些優(yōu)化的存儲(chǔ)過程在DataContext里面封裝成方法,再調(diào)用這些方法,這樣既可以把sql寫的更好,效率又更高,而且還對(duì)提高個(gè)人的數(shù)據(jù)庫知識(shí)水平也有很大幫助。

關(guān)于一體化查詢語言LINQ的操作就介紹到這里了,希望通過本次的介紹能夠帶給您一些收獲,謝謝各位瀏覽!

【編輯推薦】

  1. SQL Server數(shù)據(jù)庫主鍵及復(fù)合主鍵的配置
  2. SQL SERVER 數(shù)據(jù)挖掘之理解內(nèi)容類型
  3. 使用SSMA將Oracle數(shù)據(jù)庫轉(zhuǎn)成SQL Server 2008
  4. SQL Server數(shù)據(jù)庫如何更改SA密碼和默認(rèn)端口號(hào)
  5. SQL Server引起CPU使用率是100%的常見原因及優(yōu)化方案
責(zé)任編輯:趙鵬 來源: 博客園
相關(guān)推薦

2009-09-07 23:09:17

2009-05-13 08:21:11

SUSELinux桌面

2009-05-13 08:22:53

SUSELinux桌面

2017-05-16 10:46:06

博陽咨詢流程管理

2023-07-19 22:13:25

一體化推送平臺(tái)

2009-12-03 15:34:41

Suse Linux

2009-07-02 09:32:00

2011-05-24 09:26:02

有線無線3G

2009-08-17 22:32:25

IT運(yùn)維管理監(jiān)控運(yùn)維一體化摩卡

2009-09-01 22:45:46

2017-10-18 22:46:57

數(shù)據(jù)中心網(wǎng)絡(luò)通信技術(shù)

2013-10-15 11:12:50

2012-05-07 17:09:52

2014-08-18 13:28:54

IT運(yùn)維

2023-09-17 17:59:28

邊緣計(jì)算調(diào)度方案

2023-11-16 13:24:39

OceanBase數(shù)據(jù)庫

2014-12-25 11:25:31

2009-03-19 09:50:00

華為機(jī)房一體化

2014-10-14 10:45:18

用友
點(diǎn)贊
收藏

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