1  LINQ TO Objects续2(代码下载)

     新建项目 linq_Ch3控制台程序

   1.1 操作字符串

       ①查找字符串中包含的大写字母,字符串是由多个char类型组成

  //1.查找字符串中包含的大写字母
  string string1 = "int I=10;string String1=(I%2==0?\"偶数\":\"奇数\")";
  //过滤字符换中所有的大写字母,判断小写用IsLetter,数字用IsDigit,标点用IsPunctuation,控制字符用IsControl,分隔符用 IsSeparator,符号类型用
  //IsSymbol,例如“=”,空白类型字符用IsWhiteSpace
  var query = from s in string1
          where char.IsUpper(s)
          select s;   Console.WriteLine("大写字母有:");   foreach (var item in query)
  {
    Console.Write(item.ToString()+"\t");
  }
    效果图:

②查找字符串中包含数字,用char.IsDigit(char类型的),写法同上

      ③查找字符串中标点符号,用char.IsPunctuation(char类型的),写法同上

      ④过滤文章中包含指定单词的句子(学习let)

          let子句

          在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用。可以使用let关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用提供的表达式的结果初始化该变量。一旦用值初始化了该范围的变量,他就不能用于存储其他值。如果该范围变量存储的是可查询的类型,则可以对齐进行查询

代码如下:

  Console.WriteLine();
  //2.过滤文章指定的单词
  string text = "While taking my boat down the inland waterway to Florida a few weeks ago, I decided to tie up at Georgetown, South Carolina, for the night and visit with an old friend. As we approached the Esso dock, I saw him through my binoculars standing there awaiting us. Tall and straight as an arrow he stood, facing a cold, penetrating wind—truly a picture of a sturdy man, even though his next birthday will make him eighty-two. Yes, the man was our elder statesman, Bernard Baruch. He loaded us into his station wagon and we were off to his famous Hobcaw Barony for dinner. We sat and talked in the great living room where many notables and statesmen, including Roosevelt and Churchill, have sat and taken their cues. In his eighty-second year, still a human dynamo, Mr. Baruch talks not of the past but of present problems and the future, deploring our ignorance of history, economics, and psychology. His only reference to the past was to tell me, with a wonderful sparkle in his eye, that he was only able to get eight quail out of the ten shots the day before. What is the secret of this great man’s value to the world at eighty-one? The answer is his insatiable desire to keep being productive. Two of the hardest things to accomplish in this world are to acquire wealth by honest effort and, having gained it, to learn how to use it properly. Recently I walked into the locker room of a rather well-known golf club after finishing a round. It was in the late afternoon and most of the members had left for their homes. But a half-dozen or so men past middle age were still seated at tables talking aimlessly and drinking more than was good for them. These same men can be found there day after day and, strangely enough, each one of these men had been a man of affairs and wealth, successful in business and respected in the community. If material prosperity were the chief requisite for happiness, then each one should have been happy. Yet, it seemed to me, something very important was missing, else there would not have been the constant effort to escape the realities of life through Scotch and soda. They knew, each one of them, that their productivity had ceased. When a fruit tree ceases to bear its fruit, it is dying. And it is even so with man. What is the answer to a long and happy existence in this world of ours? I think I found it long ago in a passage from the book, Genesis, which caught my eyes while I was thumbing through my Bible. The words were few, but they became indelibly impressed on my mind: “In the sweat of thy face shalt thou eat thy bread.” To me, that has been a challenge from my earliest recollections. In fact, the battle of life, of existence, is a challenge to everyone. The immortal words of St. Paul, too, have been and always will be a great inspiration to me. At the end of the road I want to be able to feel that I have fought a good fight, I have finished the course, I have kept the faith.";   string matches = "to"; //要查询的单词
  string[] sentences = text.Split(new char[]{'.','?','!',','}); //按句子转换成数组   var query2=from item in sentences
        let words=item.Split(new char[]{'.','?','!',';',':',',',' '},StringSplitOptions.RemoveEmptyEntries)//将句子按单词转换成数组
        where words.Contains(matches)
        select item;   Console.WriteLine("含有"+matches+"的句子是:");   foreach (var item in query2)
  {
    Console.WriteLine(item.ToString());
  }

效果图:

⑤统计每个单词在一个字符串中出现的次数(使用Distinct()去掉重复的)

       原理:将字符串转成数组A,在使用Distinct()方法获得数组A的去重版本数组B

                 然后创建一个数组C存储数量,长度肯定是数组B的长度,这样数组B和数组C的长度一样,正好一个单词,对应一个数量,方便以后输出结果

                 遍历整个数组B,获得临时单词,以这个临时值作为条件用linq在数组A中找这个单词

                 然后找到后,在循环中用数组C保存找到的符合的同一个单词的出现的频率

                 以达到数组B和数组C一一对应,方便以后输出的效果

  Console.WriteLine();

  //2.过滤文章指定的单词

  string text2 = "While taking my boat down the inland waterway to Florida a few weeks ago, I decided to tie up at Georgetown";

  //拆分成以每一个单词为一个元素的字符串数组

  string[] allWords = text2.Split(new char[] { '.', '?', '!', ';', ':', ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);

  //去掉单词数组中重复的单词,用Distinct()方法

  string[] distinctWords = allWords.Distinct().ToArray();

  //创建一个存放词频统计信息的数组,以不重复的单词的数量为长度

  int[] counts = new int[distinctWords.Length];

  //遍历每一个单词,每次以这个单词在allWords数组中查找,因为allWords没有去掉重复的

  for (int i = ; i < distinctWords.Length; i++)
  {
    string tempWord = distinctWords[i];//在不重复的单词数组中一个个获得
    var query3 = from item in allWords
            where item.ToLower() == tempWord.ToLower()
            select item;
    counts[i] = query3.Count();
  }   //输出词频统计结果
  for (int i = ; i < counts.Count(); i++)
  {
    Console.WriteLine(distinctWords[i]+"出现了"+counts[i]+"次");
  }

效果图:

同样的遍历后对比思想,我们可以查找 中文在一个字符串中的个数,这次我们使用正则

  string text3 = "衷心感谢你们对茗洋芳竹的支持,博客地址:http://www.cnblogs.com/AaronYang,谢谢你们";
  int cnt = ;   Regex rgx = new Regex("^[\u4E00-\u9FA5]{0,}$");   for (int i = ; i < text3.Length; i++)
  {
    cnt = rgx.IsMatch(text3[i].ToString()) ? ++cnt : cnt;
  }   Console.WriteLine("”" + text3 + "“中中文有" + cnt.ToString()+"个");

效果图:

那么同样,我们现在想在一篇文章中找ing结果的单词

还记得我们上篇中,在linq的条件中加一个自定义的方法,比较条件

例如:

var query5=from item in words

where item.EndsWith("ing")

select item;

同理,我们也可声明个正则,假如我们声明一个

Regex r=new Regex(@"ing\b");

然后查询中

var query5=from item in words

where r.Match(item).Success==true)

select item;

思考方式就在这里,也就是linq中找出的临时值,你可以用临时值的相关方法去判断,也可以用外面的方法,比如遍历的每一次返回都是一个对象,你想 比较这个对象中某些属性是否符合条件,一,你可以在外面写个自定一个方法,二、你在对象中写个方法,每次判断对象.方法,其实方法还有很多,这里不说了

⑤将两个集合中的元素串联起来(集合1.concat(集合2))

  int[] arr1 = { ,,,,,,,,};
  int[] arr2 = { , , , , , , };
  var arr3 = arr1.Concat(arr2);   Console.WriteLine("串联后:");   foreach (var item in arr3)
  {
    Console.Write(item+",");
  }

效果图,可说明,concat是不去重复的

方式二:

使用Array类的CopyTo方法也可以实现

  Console.WriteLine();
  int[] arr4 = new int[arr2.Length + arr1.Length];
  arr1.CopyTo(arr4, );
  arr2.CopyTo(arr4,);
  Console.WriteLine("arr4的结果:");   foreach (var item in arr4)
  {
    Console.Write(item + ",");
  }

效果图:

讲解CopyTo,数组1.CopyTo(数组2,在数组2中,数组1中的值插入的在数组2中索引位置)

arr1.CopyTo(arr4, 0);  在arr4的索引0的位置上开始复制arr1中的数据,arr1中一共9个值,也就是占据了arr4的0-8的位置

arr2.CopyTo(arr4,9);   然后在arr4的索引9的位置上开始复制arr2中的数据

如果不是9是4的话,那么arr1在arr4的4开始(包括4)的位置到索引8的位置的值都被arr2替换了

 1.2 使用LINQ操作文件,说白了也是操作集合

        准备工作:我们在d盘建立一个LinqStudy的文件夹,然后在里面新建两个文件夹,linq1和linq2,然后我么在linq1中建立几个文件,linq.doc,linq.txt,博客园.doc,博客园.txt

然后我们赋值linq.txt和博客园.doc放到linq2中去,然后我们再在linq2文件下建立 linq学习.ppt

然后我们回到linq文件夹,在linq.txt中添加点文字,改变该文件的长度大小

还是在这个解决方案里新建一个linq_ch4的控制台程序

 

    1.2.1 取两个目录中的同名文件

我们使用

public FileInfo[] GetFiles(string searchPattern)这个方法获得文件

例如,过滤d:\LinqStudy\linq1目录中所有扩展名为.txt的文件

dir.GetFiles("*.txt");

代码如下:

  string dir1 = @"D:\LinqStudy\linq1";
  string dir2 = @"D:\LinqStudy\linq2";   List<FileInfo> files1 = new List<FileInfo>();
  List<FileInfo> files2 = new List<FileInfo>();   foreach (string file in Directory.GetFiles(dir1))
  {
    files1.Add(new FileInfo(file));
  }   foreach (string file in Directory.GetFiles(dir2))
  {
    files2.Add(new FileInfo(file));
  }   var query2 = from file1 in files1
          join file2 in files2
          on file1.Name equals file2.Name
          orderby file1.Name
          select file1;   foreach (var item in query2)
  {
    Console.WriteLine(string.Format("{0}\t{1}",item.Name,item.Length));
  }

效果图:

同样我们也可以在加个条件,除了文件名一样,文件长度也要一样,才是同一个文件

效果图:

竟然获得后,我们就可以进行删除,复制等等操作了

关于FileInfo对象还有  创建时间啊,最近访问日期啊等等,例如FileInfo对象.CreateTime

    1.2.2 查找指定名称的文件

        例如我们查找文件名中还有.NET的后缀名为doc类型的文件

        var query=from file in files

                           where file.Extension==”.doc” && file.Name.IndexOf(”.NET”)>-1

                           orderby file.Name

                           select file;

       我们也可以通过这个方法获得,*号是通配符,跟sql中的*在like语句中一样的

       FileInfo[] files=dir.GetFiles(”*.NET*.doc”)  

   1.2.3 查找大文件

        例如我们想在某一个文件夹下查找1个G以上的文件  

         var query=from file in files

                           where file.Length>1000000000

                           orderby file.Length

                           select file;

         当然你也可以指定一个文件的长度的范围值,文件夹也可以指定,无指定时,你可以递归整个磁盘,然后目录然后文件,就这样查询,获得后,你也可以先只记那个文件的文件目录,或文件名显示等等

   1.2.4 查找只读文件

           var query=from file in files

                           where (file.Attributes & FileAttributes.ReadOnly)!=0

                           select file;

  1.2.4 查找包含指定内容的文件

  Console.WriteLine();
  string findStr = "LinqStudy";   Console.WriteLine("包含" + findStr + "文字的txt文件如下:");   var query3 = from file in files1
          where file.Extension == ".txt"
          let content = File.ReadAllText(file.FullName, System.Text.Encoding.Default)
          where content.Contains(findStr)
          orderby file.Name
          select file;   foreach (var item in query3)
  {
    Console.WriteLine(string.Format("{0}\t{1}", item.Name, item.Length));
  }

效果图:

原理也很简单,File.ReadAllText(文件路径,编码方式),可以获得txt文件中的文字,然后用let关键字,将内容临时存到content变量中

注意:在这个例子中,读取文件内容时,使用ReadAllText()方法将文件的内容一次性全部读取到内存,如果文件很大,那么久非常耗损系统资源,我们可以使用流的方式,每次只读取一行数据,然后逐行判断。


到此为止,linq to Objects已经讲完了

下次我们主要讲linq to SQL

那天有个小孩跟我说LINQ(三)转载的更多相关文章

  1. 那天有个小孩跟我说LINQ(五)转载

    2  LINQ TO SQL(代码下载)      我们以一个简单的销售的业务数据库为例子         表结构很简单:Users(购买者(用户)表),Products(产品信息表),Sales(销 ...

  2. 那天有个小孩跟我说LINQ(七)转载

    1  LINQ TO XML(代码下载)        准备:新建项目 linq_Ch7控制台程序,新建一个XML文件夹,我们就轻松地学习一下吧          XDocument         ...

  3. 那天有个小孩跟我说LINQ(六)转载

    2  LINQ TO SQL完结(代码下载)      我们还是接着上次那个简单的销售的业务数据库为例子,打开上次那个例子linq_Ch5 2.1 当数据库中的表建立了主外键 ①根据主键获取子表信息 ...

  4. 那天有个小孩跟我说LINQ(四)转载

    1  LINQ TO SQL(代码下载)       我们以一个酒店管理系统的数据库为例子         表结构很简单:GuestInfo(客人信息表),Room(房间表),RoomType(房间类 ...

  5. 那天有个小孩跟我说LINQ(二)转载

    1  LINQ TO Objects续(代码下载)      新建项目 linq_Ch2控制台程序,新建一个Entity文件夹    1.1 学生成绩查询(练习Join)         有三张表如下 ...

  6. 那天有个小孩跟我说LINQ(一) 转载

    1  LINQ准备(代码下载) 新建项目 linq_Ch1控制台程序,新建一个Entity文件夹     1.1 对象初始化器     在Entity新建一个类Student,代码如下 using S ...

  7. 那天有个小孩跟我说LINQ(八)学会Func

    文章已经迁移到:http://www.ayjs.net/2013/08/68/ 文章已经迁移到:http://www.ayjs.net/2013/08/68/ 文章已经迁移到:http://www.a ...

  8. 那天有个小孩教我WCF[一][2/3]

    接着上次的继续讲吧 我们开始吧 9.创建数据库 use master go --创建库 if exists(select * from sysdatabases where name='NewsDB' ...

  9. 那天有个小孩教我WCF[一][1/3]

    那天有个小孩教我WCF[一][1/3] 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不细讲,步步教你创建一个简单SOA案例,对WCF有个基本的认识,我不 ...

随机推荐

  1. 从Spring HibernateTemplate模板方法设计模式的实现谈起

    概述 模板方法模式是GOF设计模式中很典型的设计模式,其意图是由抽象父类控制顶级逻辑,并把基本操作的实现推迟到子类去实现,这是通过继承的手段来达到对象的复用.Spring模板方法模式实际是模板方法模式 ...

  2. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  3. 【原创】LoadRunner Java Vuser开发环境配置指南

    1 编写目的 本文主要介绍Java运行环境的配置,同时通过编写HelloWorld程序,讲解在LoadRunner下如何开发简单的Java Vuser脚本.关于Java语言的深入学习,大家可以参考其他 ...

  4. Appium 小白从零安装 ,Appium连接真机测试。

    以下是我个人在初次安装使用Appium时的过程,过程中遇到了一些问题,在这里也一一给出解决办法. Appium安装过程 先安装了 Node.js.在node的官网上下载的exe安装文件. 在node的 ...

  5. Linux下安装启动nginx的过程

    1.首先将nginx的安装包传到虚拟机里的/home目录下 2.为了方便nginx运行而不影响linux安全需创建组合用户 groupadd -r nginxuseradd -r -g nginx  ...

  6. synchronize学习

    这个例子我们看到,java中将对象或者Class对象当做锁 package synchronized简单使用; public class Test7 extends Thread{ public st ...

  7. oracle 分区和分区索引

    一.个人理解:建表时一般都会指定在一个表空间上,但是可能随着表空间扩大,查询越来越慢,分区表就是将一个表实际存在不同的表空间,oracle存储分为块,断,表空间.新建一个表,会给表分配指定大小的段,段 ...

  8. awk学习

    首先分享一个哥们的文章:http://coolshell.cn/articles/9070.html

  9. HDOJ-ACM1061(JAVA) Rightmost Digit

    题意:求n的n次方的个位数(1<=N<=1,000,000,000) 第一个最愚蠢的办法就是暴力破解,没什么意义,当然,还是实现来玩玩. 以下是JAVA暴力破解: import java. ...

  10. 《算法:C语言实现》阅读笔记

    //从今天起准备认真看完这本书.本渣虽然笨,但是窝懒啊.... //今天开始看第一章.希望坚持下去. 第一章 引言 通过讨论连通问题的几种算法,来引出算法的重要性. 1.1 连通问题的快速查找算法 感 ...