Skip to content

Commit

Permalink
Merge pull request #126 from matthewreily/the-last-of-the-aggregates
Browse files Browse the repository at this point in the history
Last of the LINQ Aggregate sample
  • Loading branch information
BethMassi committed Nov 17, 2015
2 parents bc39bb1 + 80d34c0 commit c31d29e
Show file tree
Hide file tree
Showing 20 changed files with 505 additions and 18 deletions.
2 changes: 1 addition & 1 deletion samples/linq/csharp/aggregate/Aggregate-Sample-1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void MethodSyntaxExample()

double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);

Console.WriteLine("Total product of all numbers: {0}", product);
Console.WriteLine($"Total product of all numbers: {product}");
}
}
}
2 changes: 1 addition & 1 deletion samples/linq/csharp/aggregate/Aggregate-Sample-2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void MethodSyntaxExample()
(balance, nextWithdrawal) =>
((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

Console.WriteLine("Ending balance: {0}", endBalance);
Console.WriteLine($"Ending balance: {endBalance}");
}
}
}
21 changes: 21 additions & 0 deletions samples/linq/csharp/aggregate/Average-Sample-1.cs
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}.");
}
}
}
77 changes: 77 additions & 0 deletions samples/linq/csharp/aggregate/Average-Sample-2.cs
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}.");
}
}
}
}
4 changes: 2 additions & 2 deletions samples/linq/csharp/aggregate/Count-Sample-1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void MethodSyntaxExample()

int uniqueFactors = factorsOf300.Distinct().Count();

Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
Console.WriteLine($"There are {uniqueFactors} unique factors of 300.");
}

//This sample uses Count to get the number of unique factors of 300 using query syntax.
Expand All @@ -29,7 +29,7 @@ public static void QuerySyntaxExample()
int uniqueFactors =
(from f in factorsOf300 select f).Distinct().Count();

Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
Console.WriteLine($"There are {uniqueFactors} unique factors of 300.");
}
}
}
2 changes: 1 addition & 1 deletion samples/linq/csharp/aggregate/Count-Sample-2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void Example()

int oddNumbers = numbers.Count(n => n%2 == 1);

Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
Console.WriteLine($"There are {oddNumbers} odd numbers in the list.");
}
}
}
2 changes: 1 addition & 1 deletion samples/linq/csharp/aggregate/Count-Sample-3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void Example()

foreach (var item in orderCounts)
{
Console.WriteLine("Customer {0} has {1} order(s).", item.Customer, item.OrderCount);
Console.WriteLine($"Customer {item.Customer} has {item.OrderCount} order(s).");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions samples/linq/csharp/aggregate/Count-Sample-4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static void QuerySyntaxExample()
from prod in products
group prod by prod.Category
into prodGroup
select new {Category = prodGroup.Key, ProductCount = prodGroup.Count()};
select new {CategoryName = prodGroup.Key, ProductCount = prodGroup.Count()};

foreach (var item in categoryCounts)
{
Console.WriteLine("There are {0} products in the {1} category.", item.ProductCount, item.Category);
Console.WriteLine($"There are {item.ProductCount} products in the {item.CategoryName} category.");
}
}

Expand All @@ -50,11 +50,11 @@ public static void MethodSyntaxExample()

var categoryCounts =
products.GroupBy(prod => prod.Category)
.Select(prodGroup => new {Category = prodGroup.Key, ProductCount = prodGroup.Count()});
.Select(prodGroup => new {CategoryName = prodGroup.Key, ProductCount = prodGroup.Count()});

foreach (var item in categoryCounts)
{
Console.WriteLine("There are {0} products in the {1} category.", item.ProductCount, item.Category);
Console.WriteLine($"There are {item.ProductCount} products in the {item.CategoryName} category.");
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions samples/linq/csharp/aggregate/Max-Sample-1.cs
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}.");
}
}
}
22 changes: 22 additions & 0 deletions samples/linq/csharp/aggregate/Max-Sample-2.cs
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.");
}
}
}
65 changes: 65 additions & 0 deletions samples/linq/csharp/aggregate/Max-Sample-3.cs
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}.");
}
}
}
}
77 changes: 77 additions & 0 deletions samples/linq/csharp/aggregate/Max-Sample-4.cs
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}.");
}
}
}
}

0 comments on commit c31d29e

Please sign in to comment.