查询LINQ字符串方法浅析
创始人
2024-06-06 21:20:19
0

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

【编辑推荐】

  1. LINQ——语言级集成查询入门指南
  2. LINQ的演变及其对C#设计的影响
  3. LINQ查询的目的与实现手段
  4. LINQ查询表达式深入剖析
  5. 实例二:绑定到LINQ查询的结果

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...