Skip to content

Commit

Permalink
Merge pull request #120 from matthewreily/even-more-aggregate-examples
Browse files Browse the repository at this point in the history
Even more aggregate examples
  • Loading branch information
BethMassi committed Nov 16, 2015
2 parents b2d3f20 + 5af09f2 commit cd63014
Show file tree
Hide file tree
Showing 14 changed files with 1,000 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ samples/MigratingFromMvc5/NewMvc6Project/src/NewMvc6Project/wwwroot/lib/
samples/AngularSample/src/AngularSample/wwwroot/lib
project.lock.json
samples/WebApplication1/src/WebApplication1/wwwroot/lib/
/samples/linq/csharp/aggregate/*.xproj
/samples/linq/csharp/aggregate/*.sln
6 changes: 3 additions & 3 deletions samples/linq/csharp/aggregate/Aggregate-Sample-1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public static class AggregateSample1
{
//This sample uses Aggregate to create a running product on the array that calculates the total product of all elements.
//Output:
//Total product of all numbers: 88.33081
// Total product of all numbers: 88.33081
public static void MethodSyntaxExample()
{
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };

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

Console.WriteLine("Total product of all numbers: {0}", product);
Console.WriteLine("Total product of all numbers: {0}", product);
}
}
}
58 changes: 29 additions & 29 deletions samples/linq/csharp/aggregate/Count-Sample-1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

namespace Aggregate
{
public static class CountSample1
{
//This sample uses Count to get the number of unique factors of 300 using method syntax.
//
// Output:
// There are 3 unique factors of 300.
public static void MethodSyntaxExample()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };
int uniqueFactors = factorsOf300.Distinct().Count();
Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}
//This sample uses Count to get the number of unique factors of 300 using query syntax.
//
// Output:
// There are 3 unique factors of 300.
public static void QuerySyntaxExample()
{
int[] factorsOf300 = { 2, 2, 3, 5, 5 };
int uniqueFactors =
(from f in factorsOf300 select f).Distinct().Count();
Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}
}
public static class CountSample1
{
//This sample uses Count to get the number of unique factors of 300 using method syntax.
//
//Output:
// There are 3 unique factors of 300.
public static void MethodSyntaxExample()
{
int[] factorsOf300 = {2, 2, 3, 5, 5};

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

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

//This sample uses Count to get the number of unique factors of 300 using query syntax.
//
//Output:
// There are 3 unique factors of 300.
public static void QuerySyntaxExample()
{
int[] factorsOf300 = {2, 2, 3, 5, 5};

int uniqueFactors =
(from f in factorsOf300 select f).Distinct().Count();

Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
}
}
}
21 changes: 21 additions & 0 deletions samples/linq/csharp/aggregate/Count-Sample-2.cs
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);
}
}
}
30 changes: 30 additions & 0 deletions samples/linq/csharp/aggregate/Count-Sample-3.cs
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);
}
}
}
}
61 changes: 61 additions & 0 deletions samples/linq/csharp/aggregate/Count-Sample-4.cs
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);
}
}
}
}
16 changes: 16 additions & 0 deletions samples/linq/csharp/aggregate/Customer.cs
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>();
}
}

0 comments on commit cd63014

Please sign in to comment.