網站首頁 編程語言 正文
LINQ的Select與SelectMany函數使用
Select擴展函數
將序列中的每個元素投影到新表單。
返回結果:
- System.Collections.Generic.IEnumerable`1 其元素是調用轉換函數的每個元素的結果 source。
Select只是每個元素獨立投影到新表單,每個元素獨自處理。
SelectMany擴展函數
一個序列的每個元素投影 System.Collections.Generic.IEnumerable`1 并將合并為一個序列將結果序列。
返回結果:? ? ? ?
- System.Collections.Generic.IEnumerable`1 其元素是一種一對多轉換函數對輸入序列中的每個元素調用的結果。
SelectMany投影后合并元素。相當于將多個集合的每一個元素全部拼接,組成一個大的集合。
測試程序如下:
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", "風晴雪,17,女", "百里屠蘇,20,男,BB" };
? ? ? ? ? ? //Select只是每個元素獨立投影到新表單,
? ? ? ? ? ? IEnumerable<string[]> selectCollection = collection.Select(person => person.Split(','));
? ? ? ? ? ? Console.WriteLine($"Select表達式的返回類型:{selectCollection.GetType()}");
? ? ? ? ? ? Console.WriteLine($"Select集合的元素個數:{selectCollection.Count()}");
? ? ? ? ? ? int index = 0;
? ? ? ? ? ? selectCollection.ToList().ForEach(p =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? Console.WriteLine($"第【{index}】個數組:其元素個數:{ p.Length}");
? ? ? ? ? ? ? ? p.ToList().ForEach(s => Console.WriteLine(" ?" + s));
? ? ? ? ? ? });
? ? ? ? ? ? Console.WriteLine("下面測試SelectMany...");
? ? ? ? ? ? //投影后合并元素。相當于將多個集合的每一個元素全部拼接,組成一個大的集合
? ? ? ? ? ? var selectMany = collection.SelectMany(person => person.Split(','));
? ? ? ? ? ? Console.WriteLine($"SelectMany表達式的返回類型:{selectMany.GetType()}");
? ? ? ? ? ? Console.WriteLine($"SelectMany集合的元素個數:{selectMany.Count()}");
? ? ? ? ? ? selectMany.ToList().ForEach(p => Console.WriteLine(p));
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}
程序運行結果截圖:?
SelectMany和Select的區別
如果我們看這兩個擴展函數的定義很容易明白——Select是把要遍歷的集合IEnumerable逐一遍歷,每次返回一個T,合并之后直接返回一個IEnumerable,而SelectMany則把原有的集合IEnumerable每個元素遍歷一遍,每次返回一個IEnumerable,把這些IEnumerable的“T”合并之后整體返回一個IEnumerable。
因此我們可以說一般情況下SelectMany用于返回一個IEnumerable<IEnumerable>的“嵌套”返回情況(把每個IEnumerable合并后返回一個整體的IEnumerable)。因此在嵌套的時候往往可以節省代碼,例如輸出帶有以下的集合:
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}
};
通常情況下要遍歷一個嵌套的數組,我們不得不采用二重循環(for或者foreach),不過現在我們可以借助SelectMany進行簡化處理(把每個內嵌的List取出,因為每一個List都是IEnumerable,合并成一個大的IEnumerable)。
簡化如下:
var result = numbers.SelectMany(collection=>collection);
foreach(var item in result)
{
? ………………
}
原文鏈接:https://blog.csdn.net/ylq1045/article/details/105119193
相關推薦
- 2022-06-22 Docker中?container?和?image?的命名_docker
- 2022-08-23 C++深入講解函數重載_C 語言
- 2022-09-20 android原生實現多線程斷點續傳功能_Android
- 2022-05-17 解決 Ubuntu 無法定位軟件包問題
- 2022-06-08 ASP.NET?Core中間件_基礎應用
- 2022-10-09 C#實現線性查找算法_C#教程
- 2022-10-02 react+typescript中使用echarts的實現步驟_React
- 2022-12-12 Android?SharedPreferences數據存儲詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支