查询LINQ字符串方法浅析

LINQ字符串的查询方式有很多,在这里介绍一种最常规的方法。如果大家有更多更好的方法,欢迎提出来一起讨论。

LINQ可用于查询和转换LINQ字符串和字符串集合。它对文本文件中的半结构化数据尤其有用。LINQ 查询可与传统的字符串函数和正则表达式结合使用。例如,可以使用 Split 或 Split 方法来创建字符串数组,然后可以使用LINQ 来查询或修改此数组。可以在 LINQ 查询的where 子句中使用IsMatch 方法。可以使用LINQ 来查询或修改由正则表达式返回的MatchCollection 结果。

LINQ查询匹配给定的字符

 
 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Collections.Generic;  
  4. using System.Collections;  
  5.  
  6.  
  7. ///   
  8. ///Class1 学习linq   
  9.  
  10. ///   
  11. public class Class1  
  12. {  
  13.  public Class1()  
  14.  {  
  15.   //  
  16.   //TODO: 在此处添加构造函数逻辑  
  17.   //  
  18.  }  
  19.     public string LinqToString()  
  20.     {  
  21.           string text = @"Historically, the world of data and the world of objects" +  
  22.           @" have not been well integrated. Programmers work in C# or Visual Basic" +  
  23.           @" and also in SQL or XQuery. On the one side are concepts such as classes," +  
  24.           @" objects, fields, inheritance, and .NET Framework APIs. On the other side" +  
  25.           @" are tables, columns, rows, nodes, and separate languages for dealing with" +  
  26.           @" them. Data types often require translation between the two worlds; there are" +  
  27.           @" different standard functions. Because the object world has no notion of query, a" +  
  28.           @" query can only be represented as a string without compile-time type checking or" +  
  29.           @" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" +  
  30.           @" objects in memory is often tedious and error-prone.";  
  31.  
  32.         string searchTerm = "data";  
  33.  
  34.         //将内容拆分成数组  
  35.         string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);  
  36.  
  37.         //从数组里查询符合条件的数据  
  38.         var matchQuery = from s in source where s.IndexOf('a')==0 orderby s ascending select s ;  
  39.  
  40.          
  41.         int wordCount = matchQuery.Count();  
  42.         string str="";  
  43.         foreach(string a in matchQuery)  
  44.             str+=a+",";  
  45.         //返回查询后的结果  
  46.         return str+":::"+wordCount;  
  47.  
  48.     }  
  49.       

本文题目:查询LINQ字符串方法浅析
网页链接:http://www.hantingmc.com/qtweb/news29/478379.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联