-
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 #120 from matthewreily/even-more-aggregate-examples
Even more aggregate examples
- Loading branch information
Showing
14 changed files
with
1,000 additions
and
35 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
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.Linq; | ||
using System; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class CountSample2 | ||
{ | ||
//This sample uses Count to get the number of odd ints in the array. | ||
// | ||
//Output: | ||
// There are 5 odd numbers in the list. | ||
public static void Example() | ||
{ | ||
int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; | ||
|
||
int oddNumbers = numbers.Count(n => n%2 == 1); | ||
|
||
Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers); | ||
} | ||
} | ||
} |
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,30 @@ | ||
using System.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
|
||
namespace Aggregate | ||
{ | ||
public static class CountSample3 | ||
{ | ||
//This sample uses Count to return a list of customers and how many orders each has. | ||
// | ||
//Output: | ||
// Customer Joe's Pizza has 1 order(s). | ||
// Customer Alfreds Futterkiste has 2 order(s). | ||
// Customer Around the Horn has 3 order(s). | ||
// Customer Bottom-Dollar Markets has 4 order(s). | ||
public static void Example() | ||
{ | ||
List<Customer> customers = Data.Customers; | ||
var orderCounts = | ||
from c in customers | ||
select new {Customer = c.CustomerName, OrderCount = c.Orders.Count()}; | ||
|
||
foreach (var item in orderCounts) | ||
{ | ||
Console.WriteLine("Customer {0} has {1} order(s).", item.Customer, item.OrderCount); | ||
} | ||
} | ||
} | ||
} |
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,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aggregate | ||
{ | ||
public static class CountSample4 | ||
{ | ||
//This sample uses Count and query syntax to return a list of product counts per category. | ||
// | ||
//Output: | ||
// There are 12 products in the Beverages category. | ||
// There are 12 products in the Condiments category. | ||
// There are 5 products in the Produce category. | ||
// There are 6 products in the Meat/Poultry category. | ||
// There are 12 products in the Seafood category. | ||
// There are 10 products in the Dairy Products category. | ||
// There are 13 products in the Confections category. | ||
// There are 7 products in the Grains/Cereals category. | ||
public static void QuerySyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categoryCounts = | ||
from prod in products | ||
group prod by prod.Category | ||
into prodGroup | ||
select new {Category = prodGroup.Key, ProductCount = prodGroup.Count()}; | ||
|
||
foreach (var item in categoryCounts) | ||
{ | ||
Console.WriteLine("There are {0} products in the {1} category.", item.ProductCount, item.Category); | ||
} | ||
} | ||
|
||
//This sample uses Count and method syntax to return a list of product counts per category. | ||
// | ||
//Output: | ||
// There are 12 products in the Beverages category. | ||
// There are 12 products in the Condiments category. | ||
// There are 5 products in the Produce category. | ||
// There are 6 products in the Meat/Poultry category. | ||
// There are 12 products in the Seafood category. | ||
// There are 10 products in the Dairy Products category. | ||
// There are 13 products in the Confections category. | ||
// There are 7 products in the Grains/Cereals category. | ||
public static void MethodSyntaxExample() | ||
{ | ||
List<Product> products = Data.Products; | ||
|
||
var categoryCounts = | ||
products.GroupBy(prod => prod.Category) | ||
.Select(prodGroup => new {Category = prodGroup.Key, ProductCount = prodGroup.Count()}); | ||
|
||
foreach (var item in categoryCounts) | ||
{ | ||
Console.WriteLine("There are {0} products in the {1} category.", item.ProductCount, item.Category); | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Aggregate | ||
{ | ||
public class Customer | ||
{ | ||
public int CustomerId { get; set; } | ||
public string CustomerName { get; set; } | ||
public string Address { get; set; } | ||
public string City { get; set; } | ||
public string PostalCode { get; set; } | ||
public string Country { get; set; } | ||
public string Phone { get; set; } | ||
public IEnumerable<Order> Orders { get; set; } = new List<Order>(); | ||
} | ||
} |
Oops, something went wrong.