现在的位置: 首页 > 综合 > 正文

101 LINQ Samples: Aggregator Operators

2012年10月03日 ⁄ 综合 ⁄ 共 4424字 ⁄ 字号 评论关闭
文章目录

Count - Simple

This sample uses Count to get the number of unique factors of 300.

  1. public void Linq73()
  2. {
  3.     int[] factorsOf300 = { 22355 };
  4.  
  5.     int uniqueFactors = factorsOf300.Distinct().Count();
  6.  
  7.     Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
  8. }

Result

There are 3 unique factors of 300.

Count - Conditional

This sample uses Count to get the number of odd ints in the array.

  1. public void Linq74()
  2. {
  3.     int[] numbers = { 5413986720 };
  4.  
  5.     int oddNumbers = numbers.Count(n => n % 2 == 1);
  6.  
  7.     Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
  8. }

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.

  1. public void Linq76()
  2. {
  3.     List<Customer> customers = GetCustomerList();
  4.  
  5.     var orderCounts =
  6.         from c in customers
  7.         select new { c.CustomerID, OrderCount = c.Orders.Count() };
  8.  
  9.     ObjectDumper.Write(orderCounts);
  10. }

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.

  1. public void Linq77()
  2. {
  3.     List<Product> products = GetProductList();
  4.  
  5.     var categoryCounts =
  6.         from p in products
  7.         group p by p.Category into g
  8.         select new { Category = g.Key, ProductCount = g.Count() };
  9.  
  10.     ObjectDumper.Write(categoryCounts
  11. }

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.


  1. public void Linq78()
  2. {
  3.     int[] numbers = { 

抱歉!评论已关闭.