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. 【VLFeat】使用matlab版本计算HOG

    下载 vlfeat-0.9.18    http://www.vlfeat.org cd D:\program\vlfeat-0.9.18\toolbox

  2. 【CSS】Intermediate1:Class and ID Selectors

    1.html tag = css selector 2.Define your own selectors in the form of class and ID selectors 3. .clas ...

  3. 洛谷P1220 关路灯

    洛谷1220 关路灯 题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关 ...

  4. 关于Eclispse连接Mysql的Jdbc

    1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql ...

  5. Google Code Jam 第一题

    通过的第一题,留做纪念,呵呵,非常简单,Africa 2010, Qualification Round: Store Credit. #include <stdio.h> #includ ...

  6. CodeForces 352D. Jeff and Furik

    题意:给n个数,第一个人选取相邻两个递降的数交换顺序,第二个人一半的概率选取相邻两个递降的数交换顺序,一半的概率选取相邻两个递增的数交换顺序.两个人轮流操作,求整个数列变成递增数列所需交换次数的期望. ...

  7. 【转】科普Spark,Spark是什么,如何使用Spark

    本博文是转自如下链接,为了方便自己查阅学习和他人交流.感谢原博主的提供! http://www.aboutyun.com/thread-6849-1-1.html http://www.aboutyu ...

  8. SNMP: Simple? Network Management Protocol(转)

    转自:http://www.rane.com/note161.html An SNMP Overview The Message Format The Actual Bytes Introductio ...

  9. 教程-(SQL DBE、ADO连接)+(Firebird火鸟+DbExpress)+(VF DBF数据库)+(DB Paradox)

    DBE 连接SQL Server显然用ADO或DBEXPRESS更有优势,起码连接起来比较方便. BDE的话可以用如下方法:(以下以Delphi7为例,其它版本的DELPHI请自己摸索一下,不过基本相 ...

  10. [USACO08JAN]电话线Telephone Lines

    多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意 ...