using System;
using System.Collections.Generic;
using System.Linq; namespace Linq101
{
class Aggregate
{
/// <summary>
/// This sample uses Count to get the number of unique factors of 300.
/// </summary>
public void Linq73()
{
int[] factorsOf300 = { , , , , }; var uniqueFactors = factorsOf300.Distinct().Count(); Console.WriteLine("There are {0} unique factors of 300.", uniqueFactors);
} /// <summary>
/// This sample uses Count to get the number of odd ints in the array.
/// </summary>
public void Linq74()
{
int[] numbers = { , , , , , , , , , }; var oddNumbers = numbers.Count(n => n % == ); Console.WriteLine("There are {0} odd numbers in the list.", oddNumbers);
} /// <summary>
/// This sample uses Count to return a list of customers and how many orders each has.
/// </summary>
public void Linq76()
{
List<Data.Customer> customers = Data.GetCustomerList(); var orderCounts = from c in customers
select new { Customer = c.CustomerID, orderCount = c.Orders.Count() }; ObjectDumper.Write(orderCounts);
} /// <summary>
/// This sample uses Count to return a list of categories and how many products each has.
/// </summary>
public void Linq77()
{
List<Data.Product> products = Data.GetProductList(); var categoryCounts = from p in products
group p by p.Category
into g
select new { Category = g.Key, Count = g.Count() }; ObjectDumper.Write(categoryCounts);
} /// <summary>
/// This sample uses Sum to get the total of the numbers in an array.
/// </summary>
public void Linq78()
{
int[] numbers = { , , , , , , , , , }; decimal total = numbers.Sum(); Console.WriteLine("The sum of the numbers is {0}", total);
} /// <summary>
/// This sample uses Sum to get the total number of characters of all words in the array.
/// </summary>
public void Linq79()
{
string[] words = { "cherry", "apple", "blueberry" }; var totalChars = words.Sum(w => w.Length); Console.WriteLine("There are a total of {0} characters in these words", totalChars);
} /// <summary>
/// This sample uses Sum to get the total units in stock for each product category.
/// </summary>
public void Linq80()
{
List<Data.Product> products = Data.GetProductList(); var categories = from p in products
group p by p.Category
into g
select new { Category = g.Key, TotalStock = g.Sum(p => p.UnitsInStock) }; ObjectDumper.Write(categories);
} /// <summary>
/// This sample uses Min to get the lowest number in an array.
/// </summary>
public void Linq81()
{
int[] numbers = { , , , , , , , , , }; int minNumber = numbers.Min(); Console.WriteLine("The minimum number is {0}", minNumber);
} /// <summary>
/// This sample uses Min to get the length of the shortest word in an array.
/// </summary>
public void Linq82()
{
string[] words = { "cherry", "apple", "blueberry" }; var shortestWord = words.Min(w => w.Length); Console.WriteLine("The shortest word is {0} characters long.", shortestWord);
} /// <summary>
/// This sample uses Min to get the cheapest price among each category's products.
/// </summary>
public void Linq83()
{
List<Data.Product> products = Data.GetProductList(); var categroys = from p in products
group p by p.Category
into g
select new { Category = g.Key, CheapestPrice = g.Min(p => p.UnitPrice) }; ObjectDumper.Write(categroys);
} /// <summary>
/// This sample uses Min to get the products with the cheapest price in each category.
/// </summary>
public void Linq84()
{
List<Data.Product> products = Data.GetProductList(); var categorys = from p in products
group p by p.Category
into g
let minPrice = g.Min(p => p.UnitPrice)
select new { Category = g.Key, CheapestProducts = g.Where(p => p.UnitPrice == minPrice) }; ObjectDumper.Write(categorys, );
} /// <summary>
/// This sample uses Max to get the highest number in an array.
/// </summary>
public void Linq85()
{
int[] numbers = { , , , , , , , , , }; int maxNum = numbers.Max(); Console.WriteLine("The maximum number is {0}", maxNum);
} /// <summary>
/// This sample uses Max to get the length of the longest word in an array.
/// </summary>
public void Linq86()
{
string[] words = { "cherry", "apple", "blueberry" }; int longestLength = words.Max(w => w.Length); Console.WriteLine("The longest word is {0} characters long.", longestLength);
} /// <summary>
/// This sample uses Max to get the most expensive price among each category's products.
/// </summary>
public void Linq87()
{
List<Data.Product> products = Data.GetProductList(); var categorys = from p in products
group p by p.Category
into g
select new { category = g.Key, price = g.Max(p => p.UnitPrice) }; ObjectDumper.Write(categorys);
} /// <summary>
/// This sample uses Max to get the products with the most expensive price in each category.
/// </summary>
public void Linq88()
{
List<Data.Product> products = Data.GetProductList(); var categorys = from p in products
group p by p.Category
into g
let maxPrice = g.Max(p => p.UnitPrice)
select new { Category = g.Key, product = g.Where(p => p.UnitPrice == maxPrice) }; ObjectDumper.Write(categorys, );
} /// <summary>
/// This sample uses Average to get the average of all numbers in an array.
/// </summary>
public void Linq89()
{
int[] numbers = { , , , , , , , , , }; double averageNumber = numbers.Average(); Console.WriteLine("The average number is {0}.", averageNumber);
} /// <summary>
/// This sample uses Average to get the average length of the words in the array.
/// </summary>
public void Linq90()
{
string[] words = { "cherry", "apple", "blueberry" }; var averageLength = words.Average(w => w.Length); Console.WriteLine("The average word length is {0} characters.", averageLength);
} /// <summary>
/// This sample uses Average to get the average price of each category's products.
/// </summary>
public void Linq91()
{
List<Data.Product> products = Data.GetProductList(); var categorys = from p in products
group p by p.Category
into g
select new { Category = g.Key, AveragePrice = g.Average(p => p.UnitPrice) }; ObjectDumper.Write(categorys, );
} /// <summary>
/// This sample uses Aggregate to create a running product on the array that calculates the total product of all elements.
/// </summary>
public void Linq92()
{
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; //var q = doubles.Aggregate((current, next) => current*next);
double product = doubles.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor); Console.WriteLine("Total product of all numbers: {0}", product);
} /// <summary>
/// This sample uses Aggregate to create a running account balance that subtracts each withdrawal from the initial balance of 100, as long as the balance never drops below 0.
/// </summary>
public void Linq93()
{
double startBalance = 100.0; int[] attemptedWithdrawals = { , , , , , , }; //double seed = 100;
//double endBalance = attemptedWithdrawals.Aggregate(seed,
// (current, next) => current - next > 0 ? current - next : current); double endBalance = attemptedWithdrawals.Aggregate(startBalance,
(banlance, nextWithdrawal) => (nextWithdrawal <= banlance) ? (banlance - nextWithdrawal) : banlance); Console.WriteLine(endBalance);
}
}
}