C#中IEnumerable<T>.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();

运行结果如下图所示:

  • 12
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值