static void Main(string[] args) {
List<Item> items=new List<Item>();
items.Add(new Item(1, "zzz"));
items.Add(new Item(2, "yyy"));
items.Add(new Item(3, "xxx"));
items.Add(new Item(1, "abc"));
items.Add(new Item(2, "kkk"));
items.Add(new Item(2, "bbb"));
items.Add(new Item(5, "mmm"));
var counters = from r in items
group r by r.ID into grp
select new { key = grp.Key, cnt = grp.Count() };
foreach (var counter in counters) {
Console.WriteLine("key=" + counter.key + ", count=" + counter.cnt);
}
or
...
var counters = items.GroupBy(r => r.ID)
.Select(group => new { key = group.Key, cnt = group.Count() });
...
both with
public class Item {
public int ID { get; set; }
public string Material { get; set; }
public Item(int ID, string material) {
this.ID = ID;
this.Material = material;
}
}