LinqToObject(2)——自由自在

做者: 梅樺 發表於 2010-05-07 10:28 原文連接 閱讀: 67 評論: 0css

對於Linq查詢有幾個要點,數據源,元素,投影。把握這幾個要點,那麼運用起來駕輕就熟。 html

(一)查找句子程序員

這裏指的句是是英文句子,英文句子以.!?結束(逗點,歎號,問號)。下面摘取《The Call of the Wild》一段來進行介紹。正則表達式

先來一大段:sql

string str=@"There he lay for the remainder of the weary night, nursing his wrath"+ide

@"and wounded pride. He could not understand what it all meant. What"+網站

@"did they want with him, these strange men? Why were they keeping"+this

@"him pent up in this narrow crate? He did not know why, but he felt"+spa

@"oppressed by the vague sense of impending calamity. Several times"+code

@"during the night he sprang to his feet when the shed door rattled open,"+

@"expecting to see the Judge, or the boys at least. But each time it was"+

@"the bulging face of the saloon-keeper that peered in at him by the sickly"+

@"light of a tallow candle. And each time the joyful bark that trembled in"+

@"Buck's throat was twisted into a savage growl.";

 

而後,查找帶有「Buck」的句子,並分別打印出來。

首先,這裏的數據源就是這個字符串(或者說這個段落),而後元素就是想要的每一個句子,投影也是句子,就是要段落中的全部句子。

獲取數據源:數據源應該是句子集合,對於字符串來講,直接的進行Linq查詢,那麼它的元素將是一個一個的字符,因此這裏要獲得數據源:

string [] sentences = str.Split( new   char []{ ' . ' , ' ? ' , ' ! ' });

 

用來分隔句子。如今的結果是:

 

There he lay for the remainder of the weary night, nursing his wrathand wounded pride

 He could not understand what it all meant
 Whatdid they want with him, these strange men
 Why were they keepinghim pent up in this narrow crate
 He did not know why, but he feltoppressed by the vague sense of impending calamity
 Several timesduring the night he sprang to his feet when the shed door rattled open,
expecting to see the Judge, or the boys at least
 But each time it wasthe bulging face of the saloon-keeper that peered in at 
him by the sicklylight of a tallow candle
 And each time the joyful bark that trembled inBuck's throat was twisted into a savage growl

 

 

而後,對於含有「Buck」的句子就是其中的元素,它的類型是字符串;而所須要就是這些句子。

var q1 = from p  in  sentences
        
where  p.Contains( " Buck " )
        select p;

  

 

 And each time the joyful bark that trembled inBuck's throat was twisted into a savage growl

  

上邊查出的句子前邊有空格,如今去除空格,這裏用正則表達式來完成。

這個相對比較簡單:

var q1 = from p  in  sentences
        
where  p.Contains( " Buck " )
        select Regex.Replace(p,
@" ^\  " , "" );

  

 

And each time the joyful bark that trembled inBuck's throat was twisted

into a savage growl

(二)遍歷文件

遍歷目錄下的全部文件並打印:

DirectoryInfo path  =   new  DirectoryInfo( @" L:\css " );
FileInfo[] fileinfos 
=  path.GetFiles(); 

var q 
=  from p  in  fileinfos
         elect 
new  { 文件名  =  p.Name, 文件大小  =  p.Length  /   1024  };
q.Dump();

 

我如今打印的是目錄下的文件:

 

文件名

文件大小

1.jpg

247

float.htm

0

……

……

連接.htm

0

 

上邊只是遍歷了目錄下的文件,而對於其中的文件夾中的文件沒能查詢到,下邊實現遍歷全路徑,方法很簡單:

DirectoryInfo path  =   new  DirectoryInfo( @" L:\css " );
FileInfo[] fileinfos 
=  path.GetFiles( " * " , SearchOption.AllDirectories); 

var q 
=  from p  in  fileinfos
         select 
new  
       { 文件名 
=  p.Name, 文件大小  =  p.Length  /   1024 , 文件路徑  =  p.FullName };

q.Dump();

   

文件名

文件大小

文件路徑

1.jpg

247

L:\css\1.jpg

float.htm

0

L:\css\float.htm

……

……

……

qq.gif

25

L:\css\pic\qq.gif

t.png

15

L:\css\pic\t.png

 

找到數據源,明確其中的元素單位,並按所需進行投影設置。

 

(三)查找最大的文件

按文件大小排序,並經過元素選擇來獲得文件

DirectoryInfo path  =   new  DirectoryInfo( @" L:\css " );
FileInfo[] fileinfos 
=  path.GetFiles( " * " , SearchOption.AllDirectories);

var q 
=  (from p  in  fileinfos
          orderby p.Length
          select p).Last();
Console.Write(q.FullName);

 

(四)在文本文件中查找字符串

查找帶有指定字符串的文件

var q  =  from p  in  fileinfos
         
where  p.Extension  ==   " .htm "
         select p.FullName;

  

先指定篩選條件文件文件,擴展名爲htm的文件,而後投影選擇文件全名。而後在投影以前添加對文本文件中的字符串的查找:

DirectoryInfo path  =   new  DirectoryInfo( @" L:\css " );
FileInfo[] files 
=  path.GetFiles( " * " , SearchOption.AllDirectories); 

var q 
=  from p  in  files
        
where  p.Extension  ==   " .htm "
        
&&  GetFileContent(p.FullName).Contains( " 導航 " )
        select p;

foreach  (var info  in  q)
    Console.WriteLine(info.FullName);

  

GetFileContent方法是經過路徑讀取文本文件內容。

結果略。

這裏提一下let關鍵字。Let相似mssql中的declare,用於聲明局部變量

var q  =  from p  in  files
         
where  p.Extension  ==   " .htm "
         let strContent
=  GetFileContent(p.FullName)
         
where  strContent.Contains( " 導航 " )
         select p;

  

這裏設置strContent變量,而後再經過另外一個where來與上過濾條件。Let很是有用。

 

評論: 0 查看評論 發表評論

程序員找工做,就在博客園

最新新聞:
· 電子商務網站之信任度(2010-10-09 17:02)
· 馬雲:管理的核心在於「抓住人性的本真」(2010-10-09 16:52)
· 另外一 Windows Phone Live 主頁截圖現身 Windows Phone 7 視頻(2010-10-09 16:38)
· 谷歌首名員工:公司成功歸結於運氣不錯(2010-10-09 16:32)
· 神奇小子Geohot帶着「limera1n」迴歸(2010-10-09 16:29)

編輯推薦:遠離.NET

網站導航:博客園首頁  我的主頁  新聞  閃存  小組  博問  社區  知識庫

相關文章
相關標籤/搜索