我有一个对象列表,如下所示:
ID:2000 Title:"Title 1" Description:"My name is Marco" ID:2000 Title:"Title 2" Description:"My name is Luca" ID:3000 Title:"Title 3" Description:"My name is Paul" ID:4000 Title:"Title 4" Description:"My name is Anthony" ID:4000 Title:"Title 5" Description:"My name is Carl" ID:4000 Title:"Title 6" Description:"My name is Jadett"
现在,我想用每个浏览(遍历)它.但我想从相同的ID开始遍历它.因此,首先是单个/唯一ID(2000,3000,4000,所以3个步骤)的foreach.对于每个“ID”步骤,每个标题/描述:对于ID 2000为2个步骤,对于ID 3000为1,对于ID 4000为3.列表按ID排序.
我该怎么做?通过…分组?呃… …
解决方法
是的,有一个小组:
foreach (var group in items.GroupBy(i => i.ID)) { foreach (var item in group) { } }