CSharp examples for LINQ:Aggregate
Counting product types in our inventory
using System;// w w w . j a v a 2 s . c o m using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var products = new List<Product> { new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "Java", OnHand = 56, UnitCost = 12.50M }, new Product { Name = "XML", OnHand = 300, UnitCost = 1.15M }, new Product { Name = "MySQL", OnHand = 0, UnitCost = 2.00M }, new Product { Name = "Whatsit", OnHand = 1, UnitCost = 1000.0M }, new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "MongoDB", OnHand = 0, UnitCost = 4.53M }, new Product { Name = "Java", OnHand = 56, UnitCost = 12.50M }, new Product { Name = "Java", OnHand = 56, UnitCost = 12.50M }, new Product { Name = "XML", OnHand = 300, UnitCost = 1.15M } }; var productTypes = products.Select(p => new { Name = p.Name }).Distinct().Count(); Console.WriteLine("\tInventory contains {0} product types", productTypes); } } class Product { public string Name; public int OnHand; public decimal UnitCost; }