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