網(wǎng)站首頁 編程語言 正文
LINQ的Select與SelectMany函數(shù)使用
Select擴(kuò)展函數(shù)
將序列中的每個元素投影到新表單。
返回結(jié)果:
- System.Collections.Generic.IEnumerable`1 其元素是調(diào)用轉(zhuǎn)換函數(shù)的每個元素的結(jié)果 source。
Select只是每個元素獨(dú)立投影到新表單,每個元素獨(dú)自處理。
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只是每個元素獨(dú)立投影到新表單,
? ? ? ? ? ? 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)。因此在嵌套的時(shí)候往往可以節(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取出,因?yàn)槊恳粋€List都是IEnumerable,合并成一個大的IEnumerable)。
簡化如下:
var result = numbers.SelectMany(collection=>collection);
foreach(var item in result)
{
? ………………
}
原文鏈接:https://blog.csdn.net/ylq1045/article/details/105119193
相關(guān)推薦
- 2022-02-28 ESlint 報(bào)錯 ESlint: this line has a lines of 103.max
- 2022-11-27 Oracle?中檢查臨時(shí)表空間的方法_oracle
- 2022-10-11 tidb-gc長時(shí)間不變動引起的慢查詢
- 2022-04-15 C語言各種操作符透徹理解下篇_C 語言
- 2022-03-26 R語言基于Keras的MLP神經(jīng)網(wǎng)絡(luò)及環(huán)境搭建_R語言
- 2024-01-28 手把手教你使用git上傳文件到倉庫
- 2022-09-20 Go?Redis客戶端使用的兩種對比_Golang
- 2022-12-05 如何在React中直接使用Redux_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支