一體化查詢語言LINQ的操作詳解
我們知道,一體化查詢語言LINQ,即(NET Language Integrated Query),是集成在 .NET Framework 3.5 編程語言中的一種新特性,已成為編程語言的一部分,使開發(fā)人員可以使用語法基本一致的語句對(duì)不同來源不同類型的數(shù)據(jù)進(jìn)行查詢與整合,它使得查詢表達(dá)式可以得到很好的編譯時(shí)語法檢查。
一:字符串查詢
- string strLinq = "Hello World!";
- var result = from q in strLinq select q;
- var result1 = from q in strLinq where q == 'o' select q;
- // Linq 的擴(kuò)展方法
- var result2 = strLinq.Where(q => q == 'o');
- var result3 = Enumerable.Where(strLinq, q => q == 'o');
二:數(shù)組查詢
- string[] strs ={ "Suyama", "Fuller", "Callahan", "Michael", "Janet" };
- var result = from p in strs where p.Length > 5 select p;
- var result1 = strs.Where(p => p.Length>5);
- var result2 = strs.Where(p =>p.StartsWith("C"));
三:XML 查詢
- <Employees>
- <Employee gender="0">
- <Name>Davolio</Name>
- <Address>507 - 20th Ave. E. Apt. 2A</Address>
- </Employee>
- <Employee gender="0">
- <Name>Andrew</Name>
- <Address>4110 Old Redmond Rd.</Address>
- </Employee>
- <Employee gender="1">
- <Name>Laura</Name>
- <Address>Coventry House Miner Rd.</Address>
- </Employee>
- <Employee gender="1">
- <Name>Anne</Name>
- <Address>4726 - 11th Ave. N.E.</Address>
- </Employee>
- <Employee gender="0">
- <Name>King</Name>
- <Address>7 Houndstooth Rd.</Address>
- </Employee>
- </Employees>
- XElement root = XElement.Load("D:\\Employees.xml");
- // 查詢性別為男性(gender=1)的全部員工
- var emps = from s in root.Elements("Employee")
- where s.Attribute("gender").Value == "1"
- select s;
- // 查詢性別為女性(gender=0)的員工的姓名和住址
- var emps1 = from s in root.Elements("Employee")
- where s.Attribute("gender").Value == "0"
- select new
- {
- name = s.Element("Name").Value,
- address = s.Element("Address").Value
- };
#p#
四:數(shù)據(jù)庫表查詢
其中用到的表的結(jié)構(gòu)以及數(shù)據(jù)大致為下圖所示:
(1) 單表查詢
- // <1> 查詢所有客戶
- var customers1 = from s in dc.Customers select s;
- List<Customer> customerLst1 = customers1.ToList();
- // <2> 查詢國籍為 'Germany' 的客戶
- var customers2 = from s in dc.Customers where s.Country == "Germany" select s;
- List<Customer> customerLst2 = customers2.ToList();
- List<Customer> customerLst2_1 = (from s in dc.Customers where s.Country == "Germany" select s).ToList();
- List<Customer> customerLst2_2 = dc.Customers.Where(s => s.Country == "Germany").ToList();
- // <3> 按公司名降序排列查詢員工ID和公司
- var customerLst3 = (from s in dc.Customers
- orderby s.CompanyName descending
- select new
- {
- ID = s.CustomerID,
- Company = s.CompanyName
- }).ToList();
- // <4> 查詢公司名,并判斷其長度是不是大于20
- var customerLst4 = (from s in dc.Customers
- select new
- {
- CompanyName = s.CompanyName,
- IsThan20 = (s.CompanyName.Length > 20) ? true : false
- }).ToList();
- // <5> 按順序查詢第10到20記錄的客戶
- List<Customer> customerLst5 = (from s in dc.Customers select s).Skip(9).Take(11).ToList();
- // Skip(9): 跳過前9個(gè)
- // Take(11): 取前11條記錄
- // <6> 國籍為 Canada和USA的客戶
- var customerLst6 = from s in dc.Customers where new string[] { "Canada", "USA" }.Contains(s.Country) select s;
- // <7> 地址中有 '9'
- var customerLst7 = from s in dc.Customers where s.Address.Contains("9") select s;
- // <8> 地址以 'A' 開頭
- var customerLst8 = from s in dc.Customers where s.Address.StartsWith("A") select s;
- var customerLst8_1 = from s in dc.Customers where s.Address.IndexOf("A") == 0 select s;
- // <9> 地址以 'A' 開頭的客戶數(shù)量
- var customerLst9 = dc.Customers.Count(s => s.Address.IndexOf("A") == 0);
- // <10> 查詢最高、最低、平均、總共的付款
- var customerLst10 = dc.Customers.Select(s => s.Payment).Max();
- var customerLst10_1 = Enumerable.Select(dc.Customers, s => s.Payment).Max();
- var customerLst10_2 = dc.Customers.Select(s => s.Payment).Min();
- var customerLst10_3 = dc.Customers.Select(s => s.Payment).Average();
- var customerLst10_4 = dc.Customers.Select(s => s.Payment).Sum();
- // <11> 按國籍查詢客戶數(shù)量和最高付款
- var customerLst11 = (from s in dc.Customers
- group s by s.Country into p
- select new
- {
- Country = p.Key,
- Count = p.Count(),
- Payment = p.Max(g => g.Payment)
- }).ToList();
#p#
(2) 多表查詢
- // <1> 查詢每個(gè)客戶下訂單的日期 (內(nèi)連接)
- var customerLst1 = from s in dc.Customers
- join p in dc.Orders on s.CustomerID equals p.CustomerID
- orderby s.CustomerID ascending
- select new
- {
- ID = s.CustomerID,
- OrderDate = p.OrderDate
- };
- // <2> 查詢每個(gè)客戶下訂單的日期 (左外連接)
- var customerLst2 = from s in dc.Customers
- join p in dc.Orders on s.CustomerID equals p.CustomerID
- into sp from a in sp.DefaultIfEmpty()
- orderby s.CustomerID ascending
- select new
- {
- ID = s.CustomerID,
- OrderDate = a.OrderDate
- };
- // <3> 查詢每個(gè)客戶下訂單的日期,條件:付款大于200且訂單日期在1997-12-08以后 (多表?xiàng)l件查詢)
- var customerLst3 = from s in dc.Customers
- join p in dc.Orders on s.CustomerID equals p.CustomerID
- where s.Payment > 200 && p.OrderDate > DateTime.Parse("1997-12-08")
- orderby s.CustomerID ascending
- select new
- {
- ID = s.CustomerID,
- OrderDate = p.OrderDate
- };
實(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的操作就介紹到這里了,希望通過本次的介紹能夠帶給您一些收獲,謝謝各位瀏覽!
【編輯推薦】






