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

Linq使用Select淺談

開(kāi)發(fā) 后端
這里介紹Linq使用Select,包括介紹返回匿名類(lèi)型,比如Linq To Sql查詢(xún)數(shù)據(jù)庫(kù)的時(shí)候只返回需要的信息,下面的例子是在Northwind數(shù)據(jù)庫(kù)中查詢(xún)Customer表。

在向大家詳細(xì)介紹Linq使用Select之前,首先讓大家了解下Linq To Sql查詢(xún)數(shù)據(jù)庫(kù),然后全面介紹Linq使用Select。

下面通過(guò)一些例子來(lái)說(shuō)明怎樣Linq使用Select,參考自:LINQ Samples

1.  可以對(duì)查詢(xún)出來(lái)的結(jié)果做一些轉(zhuǎn)換,下面的例子在數(shù)組中查找以"B"開(kāi)頭的名字,然后全部轉(zhuǎn)成小寫(xiě)輸出:

  1. string[] names = { "Jack", "Bob", "Bill", "Catty", "Willam" };  
  2. var rs = from n in names  
  3. where n.StartsWith("B")  
  4. select n.ToLower();  
  5. foreach (var r in rs)  
  6. Console.WriteLine(r); 

2. 返回匿名類(lèi)型,比如Linq To Sql查詢(xún)數(shù)據(jù)庫(kù)的時(shí)候只返回需要的信息,下面的例子是在Northwind數(shù)據(jù)庫(kù)中查詢(xún)Customer表,返回所有名字以"B"開(kāi)頭的客戶的ID和名稱(chēng):

  1. NorthwindDataContext dc = new NorthwindDataContext();  
  2. var cs = from c in dc.Customers  
  3. where c.ContactName.StartsWith("B")  
  4. select new  
  5. {  
  6. CustomerID = c.CustomerID,  
  7. CustomerName = c.ContactTitle + " " + c.ContactName  
  8. };  
  9. foreach (var c in cs)  
  10. Console.WriteLine(c); 

3. 對(duì)于數(shù)組,select可以對(duì)數(shù)組元素以及索引進(jìn)行操作:

  1. string[] names = { "Jack", "Bob", "Bill", "Catty", "Willam" };  
  2. var rs = names.Select((name, index) => new { Name = nameIndex = index });  
  3. foreach (var r in rs)  
  4. Console.WriteLine(r);  

4. 組合查詢(xún),可以對(duì)多個(gè)數(shù)據(jù)源進(jìn)行組合條件查詢(xún)(相當(dāng)于Linq使用SelectMany函數(shù)),下面的例子其實(shí)就相對(duì)于一個(gè)雙重循環(huán)遍歷:

  1. int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };  
  2. int[] numbersB = { 1, 3, 5, 7, 8 };  
  3.  
  4. var pairs =  
  5. from a in numbersA,  
  6. b in numbersB  
  7. where a < b 
  8. select new {a, b};  
  9.  
  10. Console.WriteLine("Pairs where a < b:");  
  11. foreach (var pair in pairs)  
  12. Console.WriteLine("{0} is less than {1}", pair.a, pair.b); 

而用Linq To Sql的話,相當(dāng)于進(jìn)行一次子查詢(xún):

  1. NorthwindDataContext dc = new NorthwindDataContext();  
  2. var rs = from c in dc.Customers  
  3. from o in c.Orders  
  4. where o.ShipCity.StartsWith("B")  
  5. select new { CustomerName = c.ContactName, OrderID = o.OrderID };  
  6.  
  7. foreach (var r in rs)  
  8. Console.WriteLine(r); 

【編輯推薦】

  1. LINQ to SQL Table淺談
  2. Linq語(yǔ)句問(wèn)題的解決方法
  3. Ling to sql更新實(shí)體概述
  4. Linq實(shí)體繼承簡(jiǎn)單描述
  5. Linq Library概述
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-09-09 15:44:22

Linq DataCo

2009-09-15 16:31:15

LINQ Custom

2009-09-11 11:25:35

LINQ函數(shù)集合

2009-09-10 11:29:00

LINQ to SQL

2009-09-07 17:32:14

LINQ檢索數(shù)據(jù)

2009-09-08 15:19:52

Linq Where操

2009-09-14 09:49:08

Linq擴(kuò)展函數(shù)

2009-09-16 11:15:52

Linq聯(lián)接數(shù)據(jù)

2009-09-16 11:19:48

Linq Select

2009-09-15 11:08:01

LinQ調(diào)用存儲(chǔ)過(guò)程

2009-09-09 11:07:52

LINQ to SQL

2009-06-18 10:07:44

LINQ to ACC

2009-09-14 15:45:28

LINQ刪除XML節(jié)點(diǎn)

2009-09-18 16:32:51

Linq委托實(shí)例化

2009-09-08 16:55:01

Linq實(shí)現(xiàn)XML轉(zhuǎn)換

2009-09-14 13:30:04

Linq數(shù)據(jù)和對(duì)象

2009-09-14 14:01:21

LINQ泛型數(shù)據(jù)集

2009-09-11 10:01:57

Linq對(duì)象初始值

2009-09-08 09:48:34

LINQ初始化數(shù)組

2009-09-09 13:01:33

LINQ Lambda
點(diǎn)贊
收藏

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