背景介绍
在实际项目中,对数据进行GroupBy肯定是常用需求之一,特别是采用EF等ORM框架后隔绝了用SQL语句直接操作数据,LINQ中的GroupBy肯定是要掌握的。
首先先对一个字段GroupBy,代码如下:
static void Main(string[] args){List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "日本",City = "东京",Name = "松下"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"},new Phone { Country = "美国",City = "华盛顿",Name = "HTC"}};var Lists = PhoneLists.GroupBy(p => p.Country);foreach (var list in Lists){Console.WriteLine(list.Key + ":");foreach (var lis in list){Console.WriteLine($"{lis.Country} - {lis.City} - {lis.Name}");}}Console.Read();}
运行结果如下:
由于GroupBy支持的委托是Func<TSource,Tkey> ,返回值是IEnumerable<IGroup<Tkey,TSource>>类型,也就是我们可以自己指定任何想要的键,代码如下:
static void Main(string[] args){ List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "日本",City = "东京",Name = "松下"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"},new Phone { Country = "美国",City = "华盛顿",Name = "HTC"}};var Lists = PhoneLists.GroupBy(p => new GetGroupClass().GetGroup(p));foreach (var list in Lists){Console.WriteLine(list.Key + ":");foreach (var lis in list){Console.WriteLine($"{lis.Country} - {lis.City} - {lis.Name}");}}Console.Read();}public class GetGroupClass{public string GetGroup(Phone phone){string flag = string.Empty;if (phone.Country.Equals("中国") || phone.Country.Equals("日本")){flag = "亚洲";}else if (phone.Country.Equals("美国")){flag = "美洲";}return flag;}}
上述代码中,增加GetGroupClass 这个类,里面的GetGroup方法支持根据洲来归类,而非国家。运行效果如下图所示:
上述功能都是基于一个属性GroupBy(),多个属性GroupBy的代码如下图所示:
static void Main(string[] args){List<Phone> PhoneLists = new List<Phone>(){new Phone { Country = "中国", City = "北京", Name = "小米" },new Phone { Country = "中国",City = "北京",Name = "华为"},new Phone { Country = "中国",City = "北京",Name = "联想"},new Phone { Country = "中国",City = "台北",Name = "魅族"},new Phone { Country = "日本",City = "东京",Name = "索尼"},new Phone { Country = "日本",City = "大阪",Name = "夏普"},new Phone { Country = "日本",City = "东京",Name = "松下"},new Phone { Country = "美国",City = "加州",Name = "苹果"},new Phone { Country = "美国",City = "华盛顿",Name = "三星"},new Phone { Country = "美国",City = "华盛顿",Name = "HTC"}};var Lists = PhoneLists.GroupBy(p => new { p.Country, p.City });foreach (var list in Lists){Console.WriteLine(list.Key + ":");foreach (var lis in list){Console.WriteLine($"{lis.Country} - {lis.City} - {lis.Name}");}}Console.Read();}
返回Lists的类型如下图所示:
由此可以GroupBy可以将匿名类型作为Key,这让我有所猜测GroupBy的原理是将返回类型中的属性通过反射在源类型找到并作为Key来实现分组的,我很希望看到微软的源代码。运行效果如下图所示: