独特的通过LINQ类的属性 [英] Distinct by property of class by linq
本文介绍了独特的通过LINQ类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个集合:
List<Car> cars=new List<Car>
汽车是唯一由汽车code标识。
Cars are uniquely identified by CarCode.
我有三辆车在收集,以及两个具有相同租车codeS。
I have three cars in the collection, and two with identical CarCodes.
我如何使用LINQ到这个集合转换成汽车行驶独特的车载codeS?
How can I use LINQ to convert this collection to Cars with unique CarCodes?
推荐答案
您可以使用分组,并且从每个组获得的第一辆车:
You can use grouping, and get the first car from each group:
List<Car> distinct =
cars
.GroupBy(car => car.CarCode)
.Select(g => g.First())
.ToList();
这篇关于独特的通过LINQ类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文