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);
}
}
}

Linq101-Aggregate的更多相关文章

  1. SQL Server-聚焦查询计划Stream Aggregate VS Hash Match Aggregate(二十)

    前言 之前系列中在查询计划中一直出现Stream Aggregate,当时也只是做了基本了解,对于查询计划中出现的操作,我们都需要去详细研究下,只有这样才能对查询计划执行的每一步操作都了如指掌,所以才 ...

  2. c# Enumerable中Aggregate和Join的使用

    参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/asp.net-core-environment.html http://www.yuanjiaochen ...

  3. MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)

    聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { ...

  4. MongoDB aggregate 运用篇

    基础知识 操作符介绍: $project:包含.排除.重命名和显示字段 $match:查询,需要同find()一样的参数 $limit:限制结果数量 $skip:忽略结果的数量 $sort:按照给定的 ...

  5. 细说Linq之Aggregate

    前言 Linq中有关常见的方法我们已经玩的得心应手,而对于那些少用的却是置若罔闻(夸张了点),但只有在实际应用中绞尽脑汁想出的方法还不如内置的Linq方法来的实际和简洁,不喜勿喷,怪我见识短. 通过R ...

  6. Linq专题之提高编码效率—— 第一篇 Aggregate方法

    我们知道linq是一个很古老的东西,大家也知道,自从用了linq,我们的foreach少了很多,但有一个现实就是我们在实际应用中使用到的却是屈指可数 的几个方法,这个系列我会带领大家看遍linq,好的 ...

  7. MongoDB aggregate 运用篇 个人总结

    最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得.. 别人写过的我就不过多描述了,大家一搜能搜索到N多一样的,我写一 ...

  8. System.Linq.Enumerable 中的方法 Aggregate 函数

      语法: public static TSource Aggregate<TSource>( this IEnumerable<TSource> source, Func&l ...

  9. Mongo集合操作Aggregate

    最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得.. 别人写过的我就不过多描述了,大家一搜能搜索到N多一样的,我写一 ...

  10. 使用aggregate在MongoDB中查找重复的数据记录

    我们知道,MongoDB属于文档型数据库,其存储的文档类型都是JSON对象.正是由于这一特性,我们在Node.js中会经常使用MongoDB进行数据的存取.但由于Node.js是异步执行的,这就导致我 ...

随机推荐

  1. hadoop 2.2.0 eclipse 插件编译 及相关eclipse配置图解

    https://github.com/winghc/hadoop2x-eclipse-plugin 官网 http://kangfoo.github.io/article/2013/12/build- ...

  2. 转:USB主机控制器(Host Controller)--深入理解

    1. 主机控制器(Host Controller) • UHCI: Universal Host Controller Interface (通用主机控制接口, USB1.0/1.1)      • ...

  3. SSH2中实例化不了Action的一个原因

    <!-- 流程管理 --> <action name="flow_*" class="workFlowAction" method=" ...

  4. If one session has a shared or exclusive lock on record R in an index, another session cannot insert

    If one session has a shared or exclusive lock on record R in an index, another session cannot insert ...

  5. index unique scan

    INDEX UNIQUE SCAN 索引唯一扫描.单块读 只可能发生在unique index/primary key 等值查找                      等待事件:db file s ...

  6. hg 证书验证失败

    hg clone https://bitbucket.org/pygame/pygame 出现abort: error: _ssl.c:504: error:14090086:SSL routines ...

  7. wpa_cli 连接 wifi

    转自:http://hi.baidu.com/yyangjjun/item/9dfe8e175439fc7a1009b5ba   1: run wpa_supplicant first use the ...

  8. 图论(生成树):HDU 5631Rikka with Graph

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. 【模拟】NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: n个序列.每个序列有4个值x,a,b,c,之后按照x=(a*x+b)%c扩展无穷项. 求每个序列各取一个数之后 ...

  10. HDOJ(HDU) 2162 Add ‘em(求和)

    Problem Description Write a program to determine the summation of several sets of integers. Input Th ...