分享

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

 苏霈儿咪咪 2021-03-30

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

运行结果如下图所示:

————————————————

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多