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. codeforces C. Restore Graph

    题意:构造一个有n个顶点,每个点度不超过k,然后给出每一个点到达一个定点的最短距离d数组,然后构造出这样的一个图: 思路:排序之后,有两个距离为0的或者没有直接输出-1,然后用两个游动下表,后面的与前 ...

  2. 解决weblogic Managed Server启动非常慢的情况

    jdk版本:1.7.0_79 查看控制台日志停留在如下地方: . . JAVA Memory arguments: -Xms2048m -Xmx4096m -XX:MaxPermSize=512m . ...

  3. (转载)提高mysql插入数据的速度

    (转载)http://blog.csdn.net/bhq2010/article/details/7376352 需要在mysql中插入2000万条记录,用insert语句插入速度很有限,每秒钟几百条 ...

  4. AC自动机(Aho-Corasick automation)模板 HDU:2222

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...

  5. 2012蓝桥杯本科组C/C++预赛题

    微生物增殖 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y. ...

  6. SRM 407(1-250pt, 1-500pt)

    DIV1 250pt 题意:每个员工可以有几个直系上司,也可以有几个直系下属.没有直系下属的人工资为1,有直系下属的人工资为所有直系下属工资之和.求所有人工资之和.人数 <= 50. 解法:直接 ...

  7. Xcode7连接网络设置

    XCode7连接互联网的时候需要再info.plist设置(之前版本都不需要)连接网络NSAppTransportSecurity  字典NSAllowsArbitraryLoads    布尔  Y ...

  8. 必胜宅急送Web app设计背后的思考

    O2O模式是餐饮业在移动消费趋势下主动拥抱互联网的方向,迎合餐饮消费者从以往经验判断为主转变为依靠移动设备.lbs.社交网络进行立体决策的过程.继App客户端之后,手机web app也逐渐成为O2O中 ...

  9. Appium服务器端从启动到case完成的活动分析

    此文的目的主要是通过分析Appium Server打印出来的log,加深对Appium Server所扮演角色的理解. 这整一个过程是由一个Test Case开始执行到结束,测试的对象是SDK自带的N ...

  10. Ternary Search Tree 应用--搜索框智能提示

    前面介绍了Ternary Search Tree和它的实现,那么可以用Ternary Search Tree来实现搜索框的只能提示,因为Ternary Search Tree的前缀匹配效率是非常高的, ...