做者: 梅桦 发表于 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查询,那么它的元素将是一个一个的字符,因此这里要获得数据源:
用来分隔句子。如今的结果是:
|
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”的句子就是其中的元素,它的类型是字符串;而所须要就是这些句子。
|
And each time the joyful bark that trembled inBuck's throat was twisted into a savage growl |
上边查出的句子前边有空格,如今去除空格,这里用正则表达式来完成。
这个相对比较简单:
|
And each time the joyful bark that trembled inBuck's throat was twisted into a savage growl |
(二)遍历文件
遍历目录下的全部文件并打印:
我如今打印的是目录下的文件:
文件名 |
文件大小 |
1.jpg |
247 |
float.htm |
0 |
…… |
…… |
连接.htm |
0 |
上边只是遍历了目录下的文件,而对于其中的文件夹中的文件没能查询到,下边实现遍历全路径,方法很简单:
文件名 |
文件大小 |
文件路径 |
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 |
找到数据源,明确其中的元素单位,并按所需进行投影设置。
(三)查找最大的文件
按文件大小排序,并经过元素选择来获得文件
(四)在文本文件中查找字符串
查找带有指定字符串的文件
先指定筛选条件文件文件,扩展名为htm的文件,而后投影选择文件全名。而后在投影以前添加对文本文件中的字符串的查找:
GetFileContent方法是经过路径读取文本文件内容。
结果略。
这里提一下let关键字。Let相似mssql中的declare,用于声明局部变量
这里设置strContent变量,而后再经过另外一个where来与上过滤条件。Let很是有用。
最新新闻:
· 电子商务网站之信任度(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