Linq101-Aggregate
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 5 namespace Linq101 6 { 7 class Aggregate 8 { 9 /// <summary> 10 /// This sample uses Count to get the number of unique factors of 300. 11 /// </summary> 12 public void Linq73() 13 { 14 int[] factorsOf300 = { 2, 2, 3, 5, 5 }; 15 16 var uniqueFactors = factorsOf300.Distinct().Count(); 17 18 Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors); 19 } 20 21 /// <summary> 22 /// This sample uses Count to get the number of odd ints in the array. 23 /// </summary> 24 public void Linq74() 25 { 26 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 27 28 var oddNumbers = numbers.Count(n => n % 2 == 1); 29 30 Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers); 31 } 32 33 /// <summary> 34 /// This sample uses Count to return a list of customers and how many orders each has. 35 /// </summary> 36 public void Linq76() 37 { 38 List<Data.Customer> customers = Data.GetCustomerList(); 39 40 var orderCounts = from c in customers 41 select new { Customer = c.CustomerID, orderCount = c.Orders.Count() }; 42 43 ObjectDumper.Write(orderCounts); 44 } 45 46 /// <summary> 47 /// This sample uses Count to return a list of categories and how many products each has. 48 /// </summary> 49 public void Linq77() 50 { 51 List<Data.Product> products = Data.GetProductList(); 52 53 var categoryCounts = from p in products 54 group p by p.Category 55 into g 56 select new { Category = g.Key, Count = g.Count() }; 57 58 ObjectDumper.Write(categoryCounts); 59 } 60 61 /// <summary> 62 /// This sample uses Sum to get the total of the numbers in an array. 63 /// </summary> 64 public void Linq78() 65 { 66 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 67 68 decimal total = numbers.Sum(); 69 70 Console.WriteLine("The sum of the numbers is {0}", total); 71 } 72 73 /// <summary> 74 /// This sample uses Sum to get the total number of characters of all words in the array. 75 /// </summary> 76 public void Linq79() 77 { 78 string[] words = { "cherry", "apple", "blueberry" }; 79 80 var totalChars = words.Sum(w => w.Length); 81 82 Console.WriteLine("There are a total of {0} characters in these words", totalChars); 83 } 84 85 /// <summary> 86 /// This sample uses Sum to get the total units in stock for each product category. 87 /// </summary> 88 public void Linq80() 89 { 90 List<Data.Product> products = Data.GetProductList(); 91 92 var categories = from p in products 93 group p by p.Category 94 into g 95 select new { Category = g.Key, TotalStock = g.Sum(p => p.UnitsInStock) }; 96 97 ObjectDumper.Write(categories); 98 } 99 100 /// <summary> 101 /// This sample uses Min to get the lowest number in an array. 102 /// </summary> 103 public void Linq81() 104 { 105 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 106 107 int minNumber = numbers.Min(); 108 109 Console.WriteLine("The minimum number is {0}", minNumber); 110 } 111 112 /// <summary> 113 /// This sample uses Min to get the length of the shortest word in an array. 114 /// </summary> 115 public void Linq82() 116 { 117 string[] words = { "cherry", "apple", "blueberry" }; 118 119 var shortestWord = words.Min(w => w.Length); 120 121 Console.WriteLine("The shortest word is {0} characters long.", shortestWord); 122 } 123 124 /// <summary> 125 /// This sample uses Min to get the cheapest price among each category's products. 126 /// </summary> 127 public void Linq83() 128 { 129 List<Data.Product> products = Data.GetProductList(); 130 131 var categroys = from p in products 132 group p by p.Category 133 into g 134 select new { Category = g.Key, CheapestPrice = g.Min(p => p.UnitPrice) }; 135 136 ObjectDumper.Write(categroys); 137 } 138 139 /// <summary> 140 /// This sample uses Min to get the products with the cheapest price in each category. 141 /// </summary> 142 public void Linq84() 143 { 144 List<Data.Product> products = Data.GetProductList(); 145 146 var categorys = from p in products 147 group p by p.Category 148 into g 149 let minPrice = g.Min(p => p.UnitPrice) 150 select new { Category = g.Key, CheapestProducts = g.Where(p => p.UnitPrice == minPrice) }; 151 152 ObjectDumper.Write(categorys, 1); 153 } 154 155 /// <summary> 156 /// This sample uses Max to get the highest number in an array. 157 /// </summary> 158 public void Linq85() 159 { 160 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 161 162 int maxNum = numbers.Max(); 163 164 Console.WriteLine("The maximum number is {0}", maxNum); 165 } 166 167 /// <summary> 168 /// This sample uses Max to get the length of the longest word in an array. 169 /// </summary> 170 public void Linq86() 171 { 172 string[] words = { "cherry", "apple", "blueberry" }; 173 174 int longestLength = words.Max(w => w.Length); 175 176 Console.WriteLine("The longest word is {0} characters long.", longestLength); 177 } 178 179 /// <summary> 180 /// This sample uses Max to get the most expensive price among each category's products. 181 /// </summary> 182 public void Linq87() 183 { 184 List<Data.Product> products = Data.GetProductList(); 185 186 var categorys = from p in products 187 group p by p.Category 188 into g 189 select new { category = g.Key, price = g.Max(p => p.UnitPrice) }; 190 191 ObjectDumper.Write(categorys); 192 } 193 194 /// <summary> 195 /// This sample uses Max to get the products with the most expensive price in each category. 196 /// </summary> 197 public void Linq88() 198 { 199 List<Data.Product> products = Data.GetProductList(); 200 201 var categorys = from p in products 202 group p by p.Category 203 into g 204 let maxPrice = g.Max(p => p.UnitPrice) 205 select new { Category = g.Key, product = g.Where(p => p.UnitPrice == maxPrice) }; 206 207 ObjectDumper.Write(categorys, 1); 208 } 209 210 /// <summary> 211 /// This sample uses Average to get the average of all numbers in an array. 212 /// </summary> 213 public void Linq89() 214 { 215 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 216 217 double averageNumber = numbers.Average(); 218 219 Console.WriteLine("The average number is {0}.", averageNumber); 220 } 221 222 /// <summary> 223 /// This sample uses Average to get the average length of the words in the array. 224 /// </summary> 225 public void Linq90() 226 { 227 string[] words = { "cherry", "apple", "blueberry" }; 228 229 var averageLength = words.Average(w => w.Length); 230 231 Console.WriteLine("The average word length is {0} characters.", averageLength); 232 } 233 234 /// <summary> 235 /// This sample uses Average to get the average price of each category's products. 236 /// </summary> 237 public void Linq91() 238 { 239 List<Data.Product> products = Data.GetProductList(); 240 241 var categorys = from p in products 242 group p by p.Category 243 into g 244 select new { Category = g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; 245 246 ObjectDumper.Write(categorys, 1); 247 } 248 249 /// <summary> 250 /// This sample uses Aggregate to create a running product on the array that calculates the total product of all elements. 251 /// </summary> 252 public void Linq92() 253 { 254 double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; 255 256 //var q = doubles.Aggregate((current, next) => current*next); 257 double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor); 258 259 Console.WriteLine("Total product of all numbers: {0}", product); 260 } 261 262 /// <summary> 263 /// This sample uses Aggregate to create a running account balance that subtracts each withdrawal from the initial balance of 100, as long as the balance never drops below 0. 264 /// </summary> 265 public void Linq93() 266 { 267 double startBalance = 100.0; 268 269 int[] attemptedWithdrawals = { 20, 10, 40, 50, 10, 70, 30 }; 270 271 //double seed = 100; 272 //double endBalance = attemptedWithdrawals.Aggregate(seed, 273 // (current, next) => current - next > 0 ? current - next : current); 274 275 double endBalance = attemptedWithdrawals.Aggregate(startBalance, 276 (banlance, nextWithdrawal) => (nextWithdrawal <= banlance) ? (banlance - nextWithdrawal) : banlance); 277 278 Console.WriteLine(endBalance); 279 } 280 } 281 }