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. NSValue NSNumber NSData类

    NSValue NSNumber NSData类 步骤1 NSValue 我们先看看NSValue能做什么: 一个NSValue对象是用来存储一个C或者Objective-C数据的简单容器.它可以保存 ...

  2. Prime Path(poj 3126)

    Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...

  3. 人在江湖飘,哪能不挨刀。CENTOS之后,UBUNTU,FEDORA都要安装起来作测试啊

    还好,我们有VIRTUAL BOX.

  4. CN消息的来源——父窗口不知道怎么处理,于是把这个消息加上CN_BASE在分发到实际的子窗体

    VCL存在一些非API消息以供其内部使用,为什么要这样做呢?这要从WM_COMMAND & WM_NOTIFY消息说起,我们说WM_COMMAND消息并不是直接发给实际产生消息的窗体,而是发送 ...

  5. Unity 动态载入Panel并实现淡入淡出

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://tieba.baidu.com/p/3206366700,作者:百度贴吧 水岸上 动态载入NGUI控件,这里用Panel为例说 ...

  6. 使用eclipse搭建嵌入式开发环境

    下载jdk http://download.oracle.com/otn-pub/java/jdk/7u4-b20/jdk-7u4-linux-i586.tar.gz 下载eclipse-cpp-ga ...

  7. cache的工作原理

    http://www.360doc.com/content/11/0307/21/3791508_99049437.shtml TLB(Translation Lookaside Buffer,也称快 ...

  8. 关于 NoSQL 数据库你应该了解的 10 件事

    四分之一个世纪以来,关系型数据库(RDBMS)一直是主流数据库模型.但是现在非关系型数据库,“云”或者“NoSQL”数据库,正在作为一种替代数据库模型获得越来越多的占有率.本文中我们将关注非关系型 N ...

  9. redis学习心得之二【redis主从配置】

    在前一节我们已经实践启动了一个redis服务,我们将其作为主机,现为其创建一个从机作备份使用 1.复制一份配置出来为从机所用 ~$ cp       redis/etc/redis.conf     ...

  10. Python入门基础教程(儿童版) [分享一本入门级教程]

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1. 推荐书名 No Starch--Python for ...