C#中IEnumerableT.Select()、SelectMany()的简单使用
生活随笔
收集整理的這篇文章主要介紹了
C#中IEnumerableT.Select()、SelectMany()的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文主要用來記錄、讓自己有所了解和提升,以后遺忘時可以查看,關于SelectMany(),這篇文章寫得不錯,值得一看。
話不多說,先上代碼看?Select()
public class Person {public string Name { get; set; }public string Gender { get; set; }public int Age { get; set; }public List<Phone> Phones { get; set; } }public class Phone {public string Country { get; set; }public string City { get; set; }public string Name { get; set; } }static void Main(string[] args) {List<Person> PersonLists = new List<Person>(){new Person { Name = "張三",Age = 20,Gender = "男",Phones = new List<Phone> {new Phone { Country = "中國", City = "北京", Name = "小米" },new Phone { Country = "中國",City = "北京",Name = "華為"},new Phone { Country = "中國",City = "北京",Name = "聯想"},new Phone { Country = "中國",City = "臺北",Name = "魅族"},}},new Person { Name = "松下",Age = 30,Gender = "男",Phones = new List<Phone> {new Phone { Country = "日本",City = "東京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "日本",City = "東京",Name = "松下"},}},new Person { Name = "克里斯",Age = 40,Gender = "男",Phones = new List<Phone> {new Phone { Country = "美國",City = "加州",Name = "蘋果"},new Phone { Country = "美國",City = "華盛頓",Name = "三星"},new Phone { Country = "美國",City = "華盛頓",Name = "HTC"}}}};Console.WriteLine("這是該方法的第一種重載:");var firstLists = PersonLists.Select(p => p.Name);foreach (var List in firstLists){Console.WriteLine($"{List}");}Console.WriteLine("這是該方法的第二種重載,就是加了一個索引項參數:");var secondLists = PersonLists.Select((p, q) =>{return (q.ToString() + p.Name);});foreach (var List in secondLists){Console.WriteLine($"{List}");}Console.Read(); }運行效果如下圖所示:
?接下來再看SelectMany(),SelectMany()就比較牛逼了,官方解釋為將序列的每個元素投影到?IEnumerable<TResult> 并將結果序列合并為一個序列,先看代碼和運行效果,代碼如下:
public class Person {public string Name { get; set; }public string Gender { get; set; }public int Age { get; set; }public List<Phone> Phones { get; set; } }public class Phone {public string Country { get; set; }public string City { get; set; }public string Name { get; set; } }static void Main(string[] args) {List<Person> PersonLists = new List<Person>(){new Person { Name = "張三",Age = 20,Gender = "男",Phones = new List<Phone> {new Phone { Country = "中國", City = "北京", Name = "小米" },new Phone { Country = "中國",City = "北京",Name = "華為"},new Phone { Country = "中國",City = "北京",Name = "聯想"},new Phone { Country = "中國",City = "臺北",Name = "魅族"},}},new Person { Name = "松下",Age = 30,Gender = "男",Phones = new List<Phone> {new Phone { Country = "日本",City = "東京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "日本",City = "東京",Name = "松下"},}},new Person { Name = "克里斯",Age = 40,Gender = "男",Phones = new List<Phone> {new Phone { Country = "美國",City = "加州",Name = "蘋果"},new Phone { Country = "美國",City = "華盛頓",Name = "三星"},new Phone { Country = "美國",City = "華盛頓",Name = "HTC"}}}};var Lists = PersonLists.SelectMany(p => p.Phones);//此方法的第一個重載foreach (var list in Lists){Console.WriteLine($"{list.Country} -- {list.City} --{list.Name}");}Console.Read(); }?selectMany可以將phones元素單獨投影成為一個序列:。
運行效果如下所示:
?
SelectMany()的第二種重載是這樣的:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector);保持初始化實體類數據不變,編寫第二種重載的代碼:
var Lists = PersonLists.SelectMany((p,i) => {p.Phones.ForEach(q => { q.Country += i.ToString(); });return p.Phones;});foreach (var list in Lists){Console.WriteLine($"{list.Country} -- {list.City} --{list.Name}");}Console.Read();其實無非是多了一個參數:索引,此索引為PersonLists的索引,上述代碼會在Phone元素的Country屬性中添加PersonLists的索引,返回類型依舊是,然后輸出。運行效果如下圖所示:
?
SelectMany()的第三種重載是這樣的:
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)看似十分復雜,無非就是返回一個自定義的匿名類,并且可以投影你想要的元素,編寫第三種重載的代碼:
var Lists = PersonLists.SelectMany(p => p.Phones,(p,q) => new { PersonName = p.Name,PhoneName = q.Name }); foreach (var List in Lists) {Console.WriteLine($"{List.PersonName} -- {List.PhoneName}"); } Console.Read();以上代碼的返回類型是這樣的:
運行結果如下圖所示:
?
?SelectMany()的第四種重載是這樣的:
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)其實就是比第三種又多了一個PersonLists的索引而已,代碼如下:
var Lists = PersonLists.SelectMany((p,i) => {p.Phones.ForEach(q => { q.Name += i.ToString();});return p.Phones; }, (p,q) => new { PersonName = p.Name,PhoneName = q.Name }); foreach (var List in Lists) {Console.WriteLine($"{List.PersonName} -- {List.PhoneName}"); } Console.Read();運行結果如下圖所示:
總結
以上是生活随笔為你收集整理的C#中IEnumerableT.Select()、SelectMany()的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 精通ASP.NET MVC ——路由
- 下一篇: Linux系统的服务器配置minicom