LinqToObject(1)——查詢

做者: 梅樺 發表於 2010-05-06 16:03 原文連接 閱讀: 84 評論: 0html

LINQ,語言級集成查是Language INtegrated Query的意思,它是鏈接對象領域和數據領域的一座橋樑。能夠經過C#,對各類數據源進行Linq查詢:sql

sql數據庫(LinqToSql),xml文檔(LinqToXml),ado.net的數據集,以及支持IEnumerableIEnumerable接口的任意對象集合。數據庫

Linq查詢通常分三個步驟進行:獲得數據源——創建查詢——執行查詢。下面詳細介紹查詢。這些查詢結合LinqPad來測試,關於LinqPad介紹請見:ide

http://www.cnblogs.com/jams742003/archive/2010/05/05/1728124.html測試

 

(一)基本查詢spa

基本查詢包括,獲得數據源,查詢條件,分組,排序,投影。.net

1)經過字符串來演示code

LinqPad上在statements模式下進行如下測試:orm

string  strTemp  =   " Oh,What a pity! " ;
string [] strTemps  =  strTemp.Split( new   char [] {  '   ' ' , ' ' ! '  });
 

var query 
=  from st  in  strTemps
            
where  st.Contains( ' a ' )
            orderby st.Length descending
            select st;

query.Dump();

 

能夠獲得結果:xml

 

What

a

 

說明:

·數據源strTemps

·定義變量st

·條件,where,用來查找單詞中含有字母a的單詞

·結果排序,按單詞長度降序

·投影,把單詞添充到結果query

 

如今經過匿名類型對投影進行設置:將單詞和單詞的長度作爲結果進行填充

string  strTemp  =   " Oh,What a pity! " ;
string [] strTemps  =  strTemp.Split( new   char [] {  '   ' ' , ' ' ! '  }); 

var query 
=  from st  in  strTemps
            
where  st.Contains( ' a ' )
            orderby st.Length descending
            select 
new  { 含有字母a的單詞  =  st, 單詞的長度  =  st.Length };

query.Dump();

 

結果:

 

含有字母a的單詞

單詞的長度

What

4

a

1

 

上邊兩種都是以語句方式進行的查詢,也能夠以方法方式進行查詢:

string  strTemp  =   " Oh,What a pity! " ;
string [] strTemps  =  strTemp.Split( new   char [] {  '   ' ' , ' ' ! '  }); 

var query 
=  strTemps
            .Where(st 
=>  st.Contains( ' a ' ))
            .OrderBy(st 
=>  st.Length)
            .Select(st 
=>  st);

query.Dump();

 

結果同上,

string  strTemp  =   " Oh,What a pity! " ;
string [] strTemps  =  strTemp.Split( new   char [] {  '   ' ' , ' ' ! '  }); 

var query 
=  strTemps
             .Where(st 
=>  st.Contains( ' a ' ))
             .OrderBy(st 
=>  st.Length)
             .Select(st
=> new {含有字母a的單詞  =  st, 單詞的長度  =  st.Length});

query.Dump();

 

經過方法進行查詢時須要Lambda表達式,關於Lambda請見:

http://www.cnblogs.com/jams742003/archive/2009/12/23/1630737.html

其中的方法指的是擴展方法

 

下面,實現分組,爲了演示分組功能,如今經過數據庫來實現。

 

Unid

Name

Version

2

宋江

5

38

張青

1

40

張清

1

41

ww

0

 

這張表是數據,如今以Version來分組:

from c  in  Customers
group c by c.Version into selfGroup
where  selfGroup.Count() > 1
select selfGroup

 

查詢以Version進行分組,且經過into selfGroup來對分組進行後續篩選,選擇組內成員數在大於1的,結果是:

 

Key=

1

 

 

Unid

FirstName

LastName

CreateTime

Address

Version

38

2010-1-6 9:40:47

清河縣1

1

40

2010-1-6 10:00:32

未知1

1

78

 

 

 

 

2

 

如下經過數據功能來介紹查詢方法

(二)排序

方法OrderByDescendingOrderByThenByThenByDescending

降序排列

int [] ii  =   new   int [] {  1 90 12 25 55  };
var q
= ii 
    .OrderByDescending(p
=> p)
    .Select(p
=> p);
q.Dump();

 

結果:

 

90

55

25

12

1

 

(三)結果集操做

·Distinct:去重

·Except:返回一集合中存在,另外一集合不存在的結果

·Intersect:交集

·Union:並集

 

string  str1 = " abc123aab " ;
string  str2 = " abc456 "

// Distinct
var q1 = str1.Distinct();
q1.Dump();

  

a

b

c

1

2

3

 

// Except
var q2 = str1.Except(str2);
q2.Dump();

 

1

2

3

 

// Intersect
var q3 = str1.Intersect(str2);
q3.Dump();

  

a

b

c

 

// Union
var q4 = str1.Union(str2);
q4.Dump();

 

a

b

c

1

2

3

4

5

6

 

(四)限定符

AnyAllContains

Any用於判斷是否存在元素

All 用於判斷數據源中的元素是否所有知足條件

Contains用於判斷數據源是否包含指定的元素

相關文章
相關標籤/搜索