-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from matthewreily/the-last-of-the-aggregates
Last of the LINQ Aggregate sample
- Loading branch information
Showing
20 changed files
with
505 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class AverageSample1 | ||
{ | ||
//This sample uses Average to get the average of all numbers in an array. | ||
// | ||
//Outputs: | ||
// The average number is 4.5. | ||
public static void Example() | ||
{ | ||
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; | ||
|
||
double averageNum = numbers.Average(); | ||
|
||
Console.WriteLine($"The average number is {averageNum}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class AverageSample2 | ||
{ | ||
//This sample uses Average to get the average length of the words in the array. | ||
// | ||
//Outputs: | ||
// The average word length is 6.66666666666667 characters. | ||
public static void Example() | ||
{ | ||
string[] words = { "cherry", "apple", "blueberry" }; | ||
|
||
double averageLength = words.Average(w => w.Length); | ||
|
||
Console.WriteLine("The average word length is {0} characters.", averageLength); | ||
} | ||
} | ||
|
||
public static class AverageSample3 | ||
{ | ||
//This sample uses Average and query syntax to get the average price of each category's products. | ||
// | ||
//Outputs: | ||
// The average price for a product in the Beverages category is $37.98. | ||
// The average price for a product in the Condiments category is $23.06. | ||
// The average price for a product in the Produce category is $32.37. | ||
// The average price for a product in the Meat/Poultry category is $54.01. | ||
// The average price for a product in the Seafood category is $20.68. | ||
// The average price for a product in the Dairy Products category is $28.73. | ||
// The average price for a product in the Confections category is $25.16. | ||
// The average price for a product in the Grains/Cereals category is $20.25. | ||
public static void QuerySyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
from prod in products | ||
group prod by prod.Category into prodGroup | ||
select new { CategoryName = prodGroup.Key, AveragePrice = prodGroup.Average(p => p.UnitPrice) }; | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine($"The average price for a product in the {category.CategoryName} category is {category.AveragePrice:C}."); | ||
} | ||
} | ||
|
||
//This sample uses Average and method syntax to get the average price of each category's products. | ||
// | ||
//Outputs: | ||
// The average price for a product in the Beverages category is $37.98. | ||
// The average price for a product in the Condiments category is $23.06. | ||
// The average price for a product in the Produce category is $32.37. | ||
// The average price for a product in the Meat/Poultry category is $54.01. | ||
// The average price for a product in the Seafood category is $20.68. | ||
// The average price for a product in the Dairy Products category is $28.73. | ||
// The average price for a product in the Confections category is $25.16. | ||
// The average price for a product in the Grains/Cereals category is $20.25. | ||
public static void MethodSyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
products.GroupBy(prod => prod.Category) | ||
.Select( | ||
prodGroup => new {CategoryName = prodGroup.Key, AveragePrice = prodGroup.Average(p => p.UnitPrice)}); | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine($"The average price for a product in the {category.CategoryName} category is {category.AveragePrice:C}."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class MaxSample1 | ||
{ | ||
//This sample uses Max to get the highest number in an array. Note that the method returns a single value. | ||
// | ||
//Outputs: | ||
// The maximum number is 9. | ||
public static void Example() | ||
{ | ||
int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; | ||
|
||
int maxNum = numbers.Max(); | ||
|
||
Console.WriteLine($"The maximum number is {maxNum}."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class MaxSample2 | ||
{ | ||
|
||
//This sample uses Max to get the length of the longest word in an array. | ||
// | ||
//Output: | ||
// The longest word is 9 characters long. | ||
public static void Example() | ||
{ | ||
string[] words = {"cherry", "apple", "blueberry"}; | ||
|
||
int longestLength = words.Max(w => w.Length); | ||
|
||
Console.WriteLine($"The longest word is {longestLength} characters long."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class MaxSample3 | ||
{ | ||
//This sample uses Max and query syntax to get the most expensive price among each category's products. | ||
// | ||
//Output: | ||
// The most expensive price for an item in the Beverages category is $263.50. | ||
// The most expensive price for an item in the Condiments category is $43.90. | ||
// The most expensive price for an item in the Produce category is $53.00. | ||
// The most expensive price for an item in the Meat/Poultry category is $123.79. | ||
// The most expensive price for an item in the Seafood category is $62.50. | ||
// The most expensive price for an item in the Dairy Products category is $55.00. | ||
// The most expensive price for an item in the Confections category is $81.00. | ||
// The most expensive price for an item in the Grains/Cereals category is $38.00. | ||
public static void QuerySyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
from prod in products | ||
group prod by prod.Category | ||
into prodGroup | ||
select new {CategoryName = prodGroup.Key, MostExpensivePrice = prodGroup.Max(p => p.UnitPrice)}; | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine( | ||
$"The most expensive price for an item in the {category.CategoryName} category is {category.MostExpensivePrice:C}."); | ||
} | ||
} | ||
|
||
//This sample uses Max and method syntax to get the most expensive price among each category's products. | ||
// | ||
//Output: | ||
// The most expensive price for an item in the Beverages category is $263.50. | ||
// The most expensive price for an item in the Condiments category is $43.90. | ||
// The most expensive price for an item in the Produce category is $53.00. | ||
// The most expensive price for an item in the Meat/Poultry category is $123.79. | ||
// The most expensive price for an item in the Seafood category is $62.50. | ||
// The most expensive price for an item in the Dairy Products category is $55.00. | ||
// The most expensive price for an item in the Confections category is $81.00. | ||
// The most expensive price for an item in the Grains/Cereals category is $38.00. | ||
public static void MethodSyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
products.GroupBy(prod => prod.Category) | ||
.Select( | ||
prodGroup => | ||
new {CategoryName = prodGroup.Key, MostExpensivePrice = prodGroup.Max(p => p.UnitPrice)}); | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine( | ||
$"The most expensive price for an item in the {category.CategoryName} category is {category.MostExpensivePrice:C}."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class MaxSample4 | ||
{ | ||
|
||
//This sample uses Max and query syntax to get the products with the most expensive price in each category. | ||
// | ||
//Output: | ||
// | ||
// The most expensive product in the Beverages category is Côte de Blaye. | ||
// The most expensive product in the Condiments category is Vegie-spread. | ||
// The most expensive product in the Produce category is Manjimup Dried Apples. | ||
// The most expensive product in the Meat/Poultry category is Th???ringer Rostbratwurst. | ||
// The most expensive product in the Seafood category is Carnarvon Tigers. | ||
// The most expensive product in the Dairy Products category is Raclette Courdavault. | ||
// The most expensive product in the Confections category is Sir Rodney's Marmalade. | ||
// The most expensive product in the Grains/Cereals category is Gnocchi di nonna Alice. | ||
public static void QuerySyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
from prod in products | ||
group prod by prod.Category | ||
into prodGroup | ||
let maxPrice = prodGroup.Max(p => p.UnitPrice) | ||
select | ||
new | ||
{ | ||
CategoryName = prodGroup.Key, | ||
MostExpensiveProducts = prodGroup.Where(p => p.UnitPrice == maxPrice) | ||
}; | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine( | ||
$"The most expensive product in the {category.CategoryName} category is {category.MostExpensiveProducts.First().ProductName}."); | ||
} | ||
} | ||
|
||
//This sample uses Max and method syntax to get the products with the most expensive price in each category. | ||
// | ||
//Output: | ||
// | ||
// The most expensive product in the Beverages category is Côte de Blaye. | ||
// The most expensive product in the Condiments category is Vegie-spread. | ||
// The most expensive product in the Produce category is Manjimup Dried Apples. | ||
// The most expensive product in the Meat/Poultry category is Th???ringer Rostbratwurst. | ||
// The most expensive product in the Seafood category is Carnarvon Tigers. | ||
// The most expensive product in the Dairy Products category is Raclette Courdavault. | ||
// The most expensive product in the Confections category is Sir Rodney's Marmalade. | ||
// The most expensive product in the Grains/Cereals category is Gnocchi di nonna Alice. | ||
public static void MethodSyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categories = | ||
products.GroupBy(prod => prod.Category) | ||
.Select(prodGroup => new {prodGroup, maxPrice = prodGroup.Max(p => p.UnitPrice)}) | ||
.Select(@t => new | ||
{ | ||
CategoryName = @t.prodGroup.Key, | ||
MostExpensiveProducts = @t.prodGroup.Where(p => p.UnitPrice == @t.maxPrice) | ||
}); | ||
|
||
foreach (var category in categories) | ||
{ | ||
Console.WriteLine( | ||
$"The most expensive product in the {category.CategoryName} category is {category.MostExpensiveProducts.First().ProductName}."); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.