本筆記摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,記錄一下學習過程以備後續查用。 一、統計單詞在字元串中出現的次數 請註意,若要執行計數,請先調用Split方法來創建詞數組。Split方法存在性能開銷,如果對字元串執行的唯一操作是計數詞, ...
本筆記摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,記錄一下學習過程以備後續查用。
一、統計單詞在字元串中出現的次數
請註意,若要執行計數,請先調用Split方法來創建詞數組。Split方法存在性能開銷,如果對字元串執行的唯一操作是計數詞,則應考慮改用Matches或
IndexOf方法。

class Program { static void Main(string[] args) { #region LINQ 統計單詞在字元串中出現的次數 const 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."; const string searchWord = "data"; //字元串轉換成數組 var source = text.Split(new[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); //創建查詢,並忽略大小寫比較。 var query = from word in source where string.Equals(word, searchWord, StringComparison.InvariantCultureIgnoreCase) select word; //統計匹配數量 var wordCount = query.Count(); Console.WriteLine($"{wordCount} occurrences(s) of the search word \"{searchWord}\" were found."); Console.Read(); #endregion } }View Code
運行結果如下:
二、查詢包含指定一組單詞的句子
此示例演示如何查找文本文件中包含指定一組單詞中每個單詞匹配項的句子。雖然在此示例中搜索條件數組是硬編碼的,但也可以在運行時動態填充此
數組。

class Program { static void Main(string[] args) { #region LINQ 查詢包含指定一組單詞的句子 const 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."; //將文本塊切割成數組 var sentences = text.Split('.', '?', '!'); //定義搜索條件,此列表可以在運行時動態添加。 string[] wordsToMatch = { "Historically", "data", "integrated" }; var query = from sentence in sentences let t = sentence.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries) where t.Distinct().Intersect(wordsToMatch).Count() == wordsToMatch.Length //去重,取交集後的數量對比。 select sentence; foreach (var sentence in query) { Console.WriteLine(sentence); } Console.Read(); #endregion } }View Code
運行結果如下:
查詢運行時首先將文本拆分成句子,然後將句子拆分成包含每個單詞的字元串數組。對於每個這樣的數組,Distinct<TSource> 方法移除所有重覆的單詞,
然後查詢對單詞數組和wordstoMatch數組執行Intersect<TSource>操作。如果交集的計數與wordsToMatch數組的計數相同,則在單詞中找到了所有的單詞,
然後返回原始句子。
在對Split的調用中,使用標點符號作為分隔符,以從字元串中移除標點符號。如果您沒有這樣做,則假如您有一個字元串“Historically,”,該字元串不會
與wordsToMatch數組中的“Historically”相匹配。根據源文本中標點的類型,您可能必須使用其他分隔符。
三、在字元串中查詢字元
因為String類實現泛型IEnumerable<T>介面,所以可以將任何字元串作為字元序列進行查詢。但是,這不是LINQ的常見用法。若要執行複雜的模式匹配操
作,請使用Regex類。
下麵的示例查詢一個字元串以確定它包含的數字的數目。

class Program { static void Main(string[] args) { #region LINQ 在字元串中查詢字元 const string source = "ABCDE99F-J74-12-89A"; //只選擇數字的字元 var digits = from character in source where char.IsDigit(character) select character; Console.Write("Digit:"); foreach (var digit in digits) { Console.Write($"{digit} "); } Console.WriteLine(); //選擇第一個"-"之前的所有字元 var query = source.TakeWhile(x => x != '-'); foreach (var character in query) { Console.Write(character); } Console.Read(); #endregion } }View Code
運行結果如下:
四、正則表達式結合LINQ查詢
此示例演示如何使用Regex類創建正則表達式以便在文本字元串中進行更複雜的匹配。使用LINQ查詢可以方便地對您要用正則表達式搜索的文件進行準確
篩選以及對結果進行加工。

class Program { static void Main(string[] args) { #region LINQ 正則表達式結合LINQ查詢 //請根據不同版本的VS進行路徑修改 const string floder = @"C:\Program Files (x86)\Microsoft Visual Studio\"; var fileInfoes = GetFiles(floder); //創建正則表達式來尋找所有的"Visual" var searchTerm = new Regex(@"http://(www.w3.org|www.npmjs.org)"); //搜索每一個“.html”文件 //通過where找到匹配項 //註意:select中的變數要求顯示聲明其類型,因為MatchCollection不是泛型IEnumerable集合。 var query = from fileInfo in fileInfoes where fileInfo.Extension == ".html" let text = File.ReadAllText(fileInfo.FullName) let matches = searchTerm.Matches(text) where matches.Count > 0 select new { name = fileInfo.FullName, matchValue = from Match match in matches select match.Value }; Console.WriteLine($"The term \"{searchTerm}\" was found in:"); Console.WriteLine(); foreach (var q in query) { //修剪匹配找到的文件中的路徑 Console.WriteLine($"name==>{q.name.Substring(floder.Length - 1)}"); //輸出找到的匹配值 foreach (var v in q.matchValue) { Console.WriteLine($"matchValue==>{v}"); } //輸出空白行 Console.WriteLine(); } Console.Read(); #endregion } /// <summary> /// 獲取指定路徑的文件信息 /// </summary> /// <param name="path"></param> /// <returns></returns> private static IList<FileInfo> GetFiles(string path) { var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); return files.Select(file => new FileInfo(file)).ToList(); } }View Code
運行結果如下:
五、查找兩個集合間的差異
此示例演示如何使用LINQ對兩個字元串列表進行比較,並輸出那些位於text1.txt中但不在text2.txt中的行。

Bankov, Peter
Holm, Michael
Garcia, Hugo
Potra, Cristina
Noriega, Fabricio
Aw, Kam Foo
Beebe, Ann
Toyoshima, Tim
Guy, Wey Yuan
Garcia, Debra
text1.txt

Liu, Jinghao
Bankov, Peter
Holm, Michael
Garcia, Hugo
Beebe, Ann
Gilchrist, Beth
Myrcha, Jacek
Giakoumakis, Leo
McLin, Nkenge
El Yassir, Mehdi
text2.txt

class Program { static void Main(string[] args) { #region LINQ 查找兩個集合間的差異 //創建數據源 var text1 = File.ReadAllLines(@"..\..\text1.txt"); var text2 = File.ReadAllLines(@"..\..\text2.txt"); //創建查詢,這裡必須使用方法語法。 var query = text1.Except(text2); //執行查詢 Console.WriteLine("The following lines are in text1.txt but not text2.txt"); foreach (var name in query) { Console.WriteLine(name); } Console.Read(); #endregion } }View Code
運行結果如下:
註:某些類型的查詢操作(如 Except<TSource>、Distinct<TSource>、Union<TSource> 和 Concat<TSource>)只能用基於方法的語法表示。
六、排序或過濾任意單詞或欄位的文本數據
下麵的示例演示如何按結構化文本(如逗號分隔值)行中的任意欄位對該文本行進行排序,可在運行時動態指定該欄位。
假定scores.csv中的欄位表示學生的ID號,後面跟著四個測驗分數。

111, 97, 92, 81, 60 112, 75, 84, 91, 39 113, 88, 94, 65, 91 114, 97, 89, 85, 82 115, 35, 72, 91, 70 116, 99, 86, 90, 94 117, 93, 92, 80, 87 118, 92, 90, 83, 78 119, 68, 79, 88, 92 120, 99, 82, 81, 79 121, 96, 85, 91, 60 122, 94, 92, 91, 91scores.csv

class Program { static void Main(string[] args) { #region LINQ 排序或過濾任意單詞或欄位的文本數據 //創建數據源 var scores = File.ReadAllLines(@"..\..\scores.csv"); //可以改為0~4的任意值 const int sortIndex = 1; //演示從方法返回查詢變數,非查詢結果。 foreach (var score in SplitSortQuery(scores, sortIndex)) { Console.WriteLine(score); } Console.Read(); #endregion } /// <summary> /// 分割字元串排序 /// </summary> /// <param name="scores"></param> /// <param name="num"></param> /// <returns></returns> private static IEnumerable<string> SplitSortQuery(IEnumerable<string> scores, int num) { var query = from line in scores let fields = line.Split(',') orderby fields[num] descending select line; return query; } }View Code
運行結果如下:
七、對一個分割的文件的欄位重新排序
逗號分隔值 (CSV) 文件是一種文本文件,通常用於存儲電子錶格數據或其他由行和列表示的表格數據。通過使用Split方法分隔欄位,可以非常輕鬆地使用
LINQ來查詢和操作CSV文件。事實上,可以使用此技術來重新排列任何結構化文本行部分。此技術不局限於CSV文件。
在下麵的示例中,假定有三列分別代表學生的“姓氏”、“名字”和“ID”,這些欄位基於學生的姓氏按字母順序排列。查詢生成一個新序列,其中首先出現的是
ID列,後面的第二列組合了學生的名字和姓氏。根據ID欄位重新排列各行,結果保存到新文件,但不修改原始數據。

Adams,Terry,120 Fakhouri,Fadi,116 Feng,Hanying,117 Garcia,Cesar,114 Garcia,Debra,115 Garcia,Hugo,118 Mortensen,Sven,113 O'Donnell,Claire,112 Omelchenko,Svetlana,111 Tucker,Lance,119 Tucker,Michael,122 Zabokritski,Eugene,121spread.csv

class Program { static void Main(string[] args) { #region LINQ 對一個分割的文件的欄位重新排序 //數據源 var lines = File.ReadAllLines(@"..\..\spread.csv"); //將舊數據的第2列的欄位放到第一位,逆向結合第0列和第1列的欄位。 var query = from line in lines let t = line.Split(',') orderby t[2] select $"{t[2]} {t[1]} {t[0]}"; foreach (var item in query) { Console.WriteLine(item); } Console.Read(); #endregion } }View Code
運行結果如下:
八、組合和比較字元串集合
此示例演示如何合併包含文本行的文件,然後排序結果。具體來說,此示例演示如何對兩組文本行執行簡單的串聯、聯合和交集。
註:text1.txt及text2.txt與五、的一致。

class Program { static void Main(string[] args) { #region LINQ 組合和比較字元串集合 var text1 = File.ReadAllLines(@"..\..\text1.txt"); var text2 = File.ReadAllLines(@"..\..\text2.txt"); //簡單連接併排序,重覆保存。 var concatQuery = text1.Concat(text2).OrderBy(x => x); OutputQueryResult(concatQuery, "Simple concatenate and sort,duplicates are preserved:"); //基於預設字元串比較器連接,並刪除重名。 var unionQuery = text1.Union(text2).OrderBy(x => x); OutputQueryResult(unionQuery, "Union removes duplicate names:"); //查找在兩個文件中出現的名稱 var intersectQuery = text1.Intersect(text2).OrderBy(x => x); OutputQueryResult(intersectQuery, "Merge based on intersect:"); //在每個列表中找到匹配的欄位,使用concat將兩個結果合併,然後使用預設的字元串比較器進行排序。 const string nameMatch = "Garcia"; var matchQuery1 = from name in text1 let t = name.Split(',') where t[0] == nameMatch select name; var matchQuery2 = from name in text2 let t = name.Split(',') where t[0] == nameMatch select name; var temp = matchQuery1.Concat(matchQuery2).OrderBy(x => x); OutputQueryResult(temp, $"Concat based on partial name match \"{nameMatch}\":"); Console.Read(); #endregion } /// <summary> /// 輸出查詢結果 /// </summary> /// <param name="querys"></param> /// <param name="title"></param> private static void OutputQueryResult(IEnumerable<string> querys, string title) { Console.WriteLine(Environment.NewLine + title); foreach (var query in querys) { Console.WriteLine(query); } Console.WriteLine($"Total {querys.Count()} names in list."); } }View Code
運行結果如下:
九、從多個源中填充對象集合
不要嘗試將記憶體中的數據或文件系統中的數據與仍在資料庫中的數據相聯接。此種跨域聯接會生成未定義的結果,因為資料庫查詢和其他類型的源定義聯
接運算的方式可能不同。另外,如果資料庫中的數據量足夠大,則存在此類運算引發記憶體不足異常的風險。若要將資料庫數據與記憶體中的數據相聯接,請首
先對資料庫查詢調用ToList或ToArray,然後對返回的集合執行聯接。

class Program { static void Main(string[] args) { #region LINQ 從多個源中填充對象集合 //spread.csv每行包含姓氏、名字和身份證號,以逗號分隔。例如,Omelchenko,Svetlana,111 var names = File.ReadAllLines(@"..\..\spread.csv"); //scores.csv每行包括身份證號碼和四個測試評分,以逗號分隔。例如,111,97,92,81,60 var scores = File.ReadAllLines(@"..\..\scores.csv"); //使用一個匿名的類型合併數據源。 //註:動態創建一個int的考試成績成員列表。 //跳過分割字元串中的第一項,因為它是學生的身份證,不是一個考試成績。 var students = from name in names let t1 = name.Split(',') from score in scores let t2 = score.Split(',') where t1[2] == t2[0] select new { FirstName = t1[0], LastName = t1[1], ID = Convert.ToInt32(t1[2]), ExamScores = (from score in t2.Skip(1) select Convert.ToInt32(score)).ToList() }; foreach (var student in students) { Console.WriteLine($"The average score of {student.FirstName} {student.LastName} is {student.ExamScores.Average()}."); } Console.Read(); #endregion } }View Code
運行結果如下:
十、使用group將一個文件拆分成多個文件
此示例演示一種進行以下操作的方法:合併兩個文件的內容,然後創建一組以新方式組織數據的新文件。
註:text1.txt及text2.txt與五、的一致。

class Program { static void Main(string[] args) { #region LINQ 使用group將一個文件拆分成多個文件 var text1 = File.ReadAllLines(@"..\..\text1.txt"); var text2 = File.ReadAllLines(@"..\..\text2.txt"); //並集:連接並刪除重覆的名字 var mergeQuery = text1.Union(text2); //根據姓氏的首字母對姓名進行分組 var query = from name in mergeQuery let t = name.Split(',') group name by t[0][0] into g orderby g.Key select g; //註意嵌套的 foreach 迴圈 foreach (var g in query) { var fileName = @"testFile_" + g.Key + ".txt"; Console.WriteLine(g.Key + ":"); //寫入文件 using (var sw = new StreamWriter(fileName)) { foreach (var name in g) { sw.WriteLine(name); Console.WriteLine(" " + name); } } } Console.Read(); #endregion } }View Code
運行結果如下:
十一、向不同的文件中加入內容
此示例演示如何聯接兩個逗號分隔文件中的數據,這兩個文件共用一個用作匹配鍵的共同值。如果您必須將兩個電子錶格的數據或一個電子錶格和一個其
他格式的文件的數據組合為一個新文件,則此技術很有用,還可以修改此示例以適合任意種類的結構化文本。

class Program { static voi