Count - Grouped

Query
public void Linq77()
{
List products = GetProductList();

var categoryCounts =
from p in products
group p by p.Category into g
select new { Category = g.Key, ProductCount = g.Count() };

ObjectDumper.Write(categoryCounts
}
Lambda Expression
public void Linq77()
{
List products = GetProductList();

var categoryCounts = products.GroupBy(prod => prod.Category).
Select(prodGroup => new {Category = prodGroup.Key, ProductCount = prodGroup.Count()});

ObjectDumper.Write(categoryCounts);
}
Output
Category=Beverages ProductCount=12
Category=Condiments ProductCount=12
Category=Produce ProductCount=5
Category=Meat/Poultry ProductCount=6
Category=Seafood ProductCount=12
Category=Dairy Products ProductCount=10
Category=Confections ProductCount=13
Category=Grains/Cereals ProductCount=7
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +