您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“LINQ如何查詢匹配給定的字符”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“LINQ如何查詢匹配給定的字符”這篇文章吧。
LINQ可用于查詢和轉(zhuǎn)換LINQ字符串和字符串集合。它對文本文件中的半結(jié)構(gòu)化數(shù)據(jù)尤其有用。LINQ 查詢可與傳統(tǒng)的字符串函數(shù)和正則表達式結(jié)合使用。例如,可以使用 Split 或 Split 方法來創(chuàng)建字符串?dāng)?shù)組,然后可以使用LINQ 來查詢或修改此數(shù)組??梢栽?LINQ 查詢的where 子句中使用IsMatch 方法??梢允褂肔INQ 來查詢或修改由正則表達式返回的MatchCollection 結(jié)果。
LINQ查詢匹配給定的字符
using System; using System.Linq; using System.Collections.Generic; using System.Collections; /// <summary> ///Class1 學(xué)習(xí)linq /// </summary> public class Class1 { public Class1() { // //TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public string LinqToString() { string text = @"Historically, the world of data and the world of objects" + @" have not been well integrated. Programmers work in C# or Visual Basic" + @" and also in SQL or XQuery. On the one side are concepts such as classes," + @" objects, fields, inheritance, and .NET Framework APIs. On the other side" + @" are tables, columns, rows, nodes, and separate languages for dealing with" + @" them. Data types often require translation between the two worlds; there are" + @" different standard functions. Because the object world has no notion of query, a" + @" query can only be represented as a string without compile-time type checking or" + @" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" + @" objects in memory is often tedious and error-prone."; string searchTerm = "data"; //將內(nèi)容拆分成數(shù)組 string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); //從數(shù)組里查詢符合條件的數(shù)據(jù) var matchQuery = from s in source where s.IndexOf('a')==0 orderby s ascending select s ; int wordCount = matchQuery.Count(); string str=""; foreach(string a in matchQuery) str+=a+","; //返回查詢后的結(jié)果 return str+":::"+wordCount; } }
以上是“LINQ如何查詢匹配給定的字符”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。