Count - Simple
This sample uses Count to get the number of unique factors of 300.
- public void Linq73()
- {
- int[] factorsOf300 = { 2, 2, 3, 5, 5 };
-
- int uniqueFactors = factorsOf300.Distinct().Count();
-
- Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
- }
Result
There are 3 unique factors of 300.
Count - Conditional
This sample uses Count to get the number of odd ints in the array.
- public void Linq74()
- {
- int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
-
- int oddNumbers = numbers.Count(n => n % 2 == 1);
-
- Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
- }
Result
There are 5 odd numbers in the list.
Count - Nested
This sample uses Count to return a list of customers and how many orders each has.
- public void Linq76()
- {
- List<Customer> customers = GetCustomerList();
-
- var orderCounts =
- from c in customers
- select new { c.CustomerID, OrderCount = c.Orders.Count() };
-
- ObjectDumper.Write(orderCounts);
- }
Result
CustomerID=ALFKI CustomerID=ANATR CustomerID=ANTON CustomerID=AROUT CustomerID=BERGS CustomerID=BLAUS CustomerID=BLONP CustomerID=BOLID CustomerID=BONAP CustomerID=BOTTM CustomerID=BSBEV CustomerID=CACTU CustomerID=CENTC CustomerID=CHOPS CustomerID=COMMI CustomerID=CONSH CustomerID=DRACD CustomerID=DUMON CustomerID=EASTC CustomerID=ERNSH CustomerID=FAMIA CustomerID=FISSA CustomerID=FOLIG CustomerID=FOLKO CustomerID=FRANK CustomerID=FRANR CustomerID=FRANS CustomerID=FURIB CustomerID=GALED CustomerID=GODOS CustomerID=GOURL CustomerID=GREAL CustomerID=GROSR CustomerID=HANAR CustomerID=HILAA CustomerID=HUNGC CustomerID=HUNGO CustomerID=ISLAT CustomerID=KOENE CustomerID=LACOR CustomerID=LAMAI CustomerID=LAUGB CustomerID=LAZYK CustomerID=LEHMS CustomerID=LETSS CustomerID=LILAS CustomerID=LINOD CustomerID=LONEP CustomerID=MAGAA CustomerID=MAISD CustomerID=MEREP CustomerID=MORGK CustomerID=NORTS CustomerID=OCEAN CustomerID=OLDWO CustomerID=OTTIK CustomerID=PARIS CustomerID=PERIC CustomerID=PICCO CustomerID=PRINI CustomerID=QUEDE CustomerID=QUEEN CustomerID=QUICK CustomerID=RANCH CustomerID=RATTC CustomerID=REGGC CustomerID=RICAR CustomerID=RICSU CustomerID=ROMEY CustomerID=SANTG CustomerID=SAVEA CustomerID=SEVES CustomerID=SIMOB CustomerID=SPECD CustomerID=SPLIR CustomerID=SUPRD CustomerID=THEBI CustomerID=THECR CustomerID=TOMSP CustomerID=TORTU CustomerID=TRADH CustomerID=TRAIH CustomerID=VAFFE CustomerID=VICTE CustomerID=VINET CustomerID=WANDK CustomerID=WARTH CustomerID=WELLI CustomerID=WHITC CustomerID=WILMK CustomerID=WOLZA |
OrderCount=6 OrderCount=4 OrderCount=7 OrderCount=13 OrderCount=18 OrderCount=7 OrderCount=11 OrderCount=3 OrderCount=17 OrderCount=14 OrderCount=10 OrderCount=6 OrderCount=1 OrderCount=8 OrderCount=5 OrderCount=3 OrderCount=6 OrderCount=4 OrderCount=8 OrderCount=30 OrderCount=7 OrderCount=0 OrderCount=5 OrderCount=19 OrderCount=15 OrderCount=3 OrderCount=6 OrderCount=8 OrderCount=5 OrderCount=10 OrderCount=9 OrderCount=11 OrderCount=2 OrderCount=14 OrderCount=18 OrderCount=5 OrderCount=19 OrderCount=10 OrderCount=14 OrderCount=4 OrderCount=14 OrderCount=3 OrderCount=2 OrderCount=15 OrderCount=4 OrderCount=14 OrderCount=12 OrderCount=8 OrderCount=10 OrderCount=7 OrderCount=13 OrderCount=5 OrderCount=3 OrderCount=5 OrderCount=10 OrderCount=9 OrderCount=0 OrderCount=6 OrderCount=10 OrderCount=6 OrderCount=9 OrderCount=13 OrderCount=28 OrderCount=5 OrderCount=18 OrderCount=12 OrderCount=11 OrderCount=10 OrderCount=5 OrderCount=6 OrderCount=31 OrderCount=9 OrderCount=7 OrderCount=4 OrderCount=9 OrderCount=12 OrderCount=4 OrderCount=3 OrderCount=5 OrderCount=10 OrderCount=7 OrderCount=3 OrderCount=11 OrderCount=10 OrderCount=4 OrderCount=10 OrderCount=15 OrderCount=9 OrderCount=14 OrderCount=8 OrderCount=7 |
Count - Grouped
This sample uses Count to return a list of categories and how many products each has.
- public void Linq77()
- {
- List<Product> 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
- }
Result
Category=Beverages Category=Condiments Category=Produce Category=Meat/Poultry Category=Seafood Category=Dairy Products Category=Confections Category=Grains/Cereals |
ProductCount=12 ProductCount=12 ProductCount=5 ProductCount=6 ProductCount=12 ProductCount=10 ProductCount=13 ProductCount=7 |
Sum - Simple
This sample uses Sum to get the total of the numbers in an array.
- public void Linq78()
- {
- int[] numbers = {