C#中LINQ的Select與SelectMany函數(shù)使用
LINQ的Select與SelectMany函數(shù)使用
Select擴(kuò)展函數(shù)
將序列中的每個元素投影到新表單。
返回結(jié)果:
- System.Collections.Generic.IEnumerable`1 其元素是調(diào)用轉(zhuǎn)換函數(shù)的每個元素的結(jié)果 source。
Select只是每個元素獨立投影到新表單,每個元素獨自處理。
SelectMany擴(kuò)展函數(shù)
一個序列的每個元素投影 System.Collections.Generic.IEnumerable`1 并將合并為一個序列將結(jié)果序列。
返回結(jié)果:
- System.Collections.Generic.IEnumerable`1 其元素是一種一對多轉(zhuǎn)換函數(shù)對輸入序列中的每個元素調(diào)用的結(jié)果。
SelectMany投影后合并元素。相當(dāng)于將多個集合的每一個元素全部拼接,組成一個大的集合。
測試程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SelectManyDemo { ? ? class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? string[] collection = new string[] { "張三,22,男", "李四,20,女,AA", "風(fēng)晴雪,17,女", "百里屠蘇,20,男,BB" }; ? ? ? ? ? ? //Select只是每個元素獨立投影到新表單, ? ? ? ? ? ? IEnumerable<string[]> selectCollection = collection.Select(person => person.Split(',')); ? ? ? ? ? ? Console.WriteLine($"Select表達(dá)式的返回類型:{selectCollection.GetType()}"); ? ? ? ? ? ? Console.WriteLine($"Select集合的元素個數(shù):{selectCollection.Count()}"); ? ? ? ? ? ? int index = 0; ? ? ? ? ? ? selectCollection.ToList().ForEach(p => ? ? ? ? ? ? { ? ? ? ? ? ? ? ? index++; ? ? ? ? ? ? ? ? Console.WriteLine($"第【{index}】個數(shù)組:其元素個數(shù):{ p.Length}"); ? ? ? ? ? ? ? ? p.ToList().ForEach(s => Console.WriteLine(" ?" + s)); ? ? ? ? ? ? }); ? ? ? ? ? ? Console.WriteLine("下面測試SelectMany..."); ? ? ? ? ? ? //投影后合并元素。相當(dāng)于將多個集合的每一個元素全部拼接,組成一個大的集合 ? ? ? ? ? ? var selectMany = collection.SelectMany(person => person.Split(',')); ? ? ? ? ? ? Console.WriteLine($"SelectMany表達(dá)式的返回類型:{selectMany.GetType()}"); ? ? ? ? ? ? Console.WriteLine($"SelectMany集合的元素個數(shù):{selectMany.Count()}"); ? ? ? ? ? ? selectMany.ToList().ForEach(p => Console.WriteLine(p)); ? ? ? ? ? ? Console.ReadLine(); ? ? ? ? } ? ? } }
程序運(yùn)行結(jié)果截圖:
SelectMany和Select的區(qū)別
如果我們看這兩個擴(kuò)展函數(shù)的定義很容易明白——Select是把要遍歷的集合IEnumerable逐一遍歷,每次返回一個T,合并之后直接返回一個IEnumerable,而SelectMany則把原有的集合IEnumerable每個元素遍歷一遍,每次返回一個IEnumerable,把這些IEnumerable的“T”合并之后整體返回一個IEnumerable。
因此我們可以說一般情況下SelectMany用于返回一個IEnumerable<IEnumerable>的“嵌套”返回情況(把每個IEnumerable合并后返回一個整體的IEnumerable)。因此在嵌套的時候往往可以節(jié)省代碼,例如輸出帶有以下的集合:
List<List<int>> numbers = new List<List<int>>() { ? new List<int>{1,2,3}, ? new List<int>{4,5,6}, ? new List<int>{7,8,9} };
通常情況下要遍歷一個嵌套的數(shù)組,我們不得不采用二重循環(huán)(for或者foreach),不過現(xiàn)在我們可以借助SelectMany進(jìn)行簡化處理(把每個內(nèi)嵌的List取出,因為每一個List都是IEnumerable,合并成一個大的IEnumerable)。
簡化如下:
var result = numbers.SelectMany(collection=>collection); foreach(var item in result) { ? ……………… }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解C#中數(shù)組、鏈表、Hash的優(yōu)缺點
在 C# 中,數(shù)組(Array)、鏈表(LinkedList)和哈希表(Hash)是常用的數(shù)據(jù)結(jié)構(gòu),每種都有其自身的優(yōu)缺點,本文將通過代碼示例給大家詳細(xì)的介紹一下,需要的朋友可以參考下2024-02-02C#?TaskScheduler任務(wù)調(diào)度器的實現(xiàn)
本文主要介紹了C#?TaskScheduler任務(wù)調(diào)度器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2023-05-05