Min - Grouped

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

var categories =
from p in products
group p by p.Category into g
select new { Category = g.Key, CheapestPrice = g.Min(p => p.UnitPrice) };

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

var categories = products.GroupBy(prod => prod.Category).Select(prodGroup => new {Category = prodGroup.Key, CheapestPrice = prodGroup.Min(p => p.UnitPrice)});

ObjectDumper.Write(categories);
}
Output
Category=Beverages CheapestPrice=4.5000
Category=Condiments CheapestPrice=10.0000
Category=Produce CheapestPrice=10.0000
Category=Meat/Poultry CheapestPrice=7.4500
Category=Seafood CheapestPrice=6.0000
Category=Dairy Products CheapestPrice=2.5000
Category=Confections CheapestPrice=9.2000
Category=Grains/Cereals CheapestPrice=7.0000
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +