欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#中IEnumerable.Select()、SelectMany()的简单使用

程序员文章站 2022-07-11 11:47:21
...

本文主要用来记录、让自己有所了解和提升,以后遗忘时可以查看,关于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();
}

运行效果如下图所示:

C#中IEnumerable.Select()、SelectMany()的简单使用

 接下来再看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元素单独投影成为一个序列:C#中IEnumerable.Select()、SelectMany()的简单使用

运行效果如下所示:

C#中IEnumerable.Select()、SelectMany()的简单使用


 

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的索引,返回类型依旧是C#中IEnumerable.Select()、SelectMany()的简单使用,然后输出。运行效果如下图所示:

C#中IEnumerable.Select()、SelectMany()的简单使用

 


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();

以上代码的返回类型是这样的:C#中IEnumerable.Select()、SelectMany()的简单使用

运行结果如下图所示:

C#中IEnumerable.Select()、SelectMany()的简单使用

 

 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#中IEnumerable.Select()、SelectMany()的简单使用