本文摘自PHP中文网,作者黄舟,侵删。
我们经常在一个内存集合,比如List,根据某个或某些个属性分组,统计显示。最容易想到的方法便是,根据某个关键属性,对List实例遍历,转化为如下字典类型1 | Dictionary< string , List<MyObject>
|
举例,已知cars,
1 2 3 4 5 6 7 8 | List<Car> cars = new List<Car>(){
new Car(1, "audiA6" , "private" ),
new Car(2, "futon" , "merchant" ),
new Car(3, "feiren" , "bike" ),
new Car(4, "bukon" , "private" ),
new Car(5, "baoma" , "private" ),
new Car(6, "dayun" , "merchant" )
};
|
想以id为键,值为Car转化为一个字典idCarDict,除了遍历这种逻辑最复杂,代码需要最多的方法,我们还可以直接利用ToDictionary方法,
1 | ar idCarDict = cars.ToDictionary(car=>car.id);
|
但是,这个方法是有局限的,关键码对应的对象实例只能有一个,也就是返回的类型为,
1 | Dictionary< string ,Object>
|
这是糟糕的,因为可能一个关键码对应多个实例,此时就得借助GroupBy,先按关键码分组后,然后再转化为字典。
比如,我们想以type为键,得到这个车型下的多台汽车,
1 2 3 | Dictionary< string , List<Car>> typeCarDict =
cars.GroupBy(car => car.type).
ToDictionary(g => g.Key, g => g.ToList());
|
这种转化代码简洁,比以下遍历逻辑好很多!
1 2 3 4 5 | var dict = new Dictionary< string ,List<Car>>(); foreach ( var car in cars)
{ if (dict.Contains(car.type))
dict[car.type].Add(car);
else
dict.Add(car.type, new List<Car>(){car}));}
|
这样解决了一个关键码对应多个实例的转化,那么根据多个关键码的组合对应多个实例的问题,借助List上的GroupBy能实现吗? 不能实现。
这时候,需要写Linq语句,将多个关键码组合为一个新的对象,
举一个列子,我们有这么一个集合,集合中的元素是ValPair对象,这个对象包含两个整形元素,Val1是小者,Val2相对大些。如何按照Val1,Val2的组合分组呢?
请看下面逻辑:
1 2 3 4 5 6 7 8 9 10 | static void printfDuplicateCompare(List<ValPair> compares)
{
var rtnByVal1 = from item in compares
group item by new { item.Val1, item.Val2 }
into g
where g.Count()>1
orderby g.Count() descending
select g;
var dict = rtnByVal1.ToDictionary(g => g.Key,g=>g.Count());
}
|
总结
List的GroupBy只能根据一个键分组,如果需要根据多个键组合分组,就得写Linq语句组合。
以上就是.NET框架-集合和LINQ中的“分组”技术代码详解的详细内容!
相关阅读 >>
.NET框架-详细介绍winform房贷分析器(图文)
.NET框架-try-parse和tester-doer的使用区别
.net中c#集合的交集、并集、差集
.NET框架-详解winform技术中组件被容器引用陷阱
.NET框架-arraylist的代码详解
.NET框架-异常设计原则详解
.NET框架-微软给出的c#编程风格代码实例
.NET框架-集合相关所有类的思维导图分享
.NET框架-clone如何由浅变深的示例代码详解
.NET框架-array的详细介绍
更多相关阅读请进入《.NET框架》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » .NET框架-集合和LINQ中的“分组”技术代码详解