Linq101-Aggregate

时间:2022-09-08 23:34:54
 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&num; 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&period;Linq&period;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. Java线程

    线程 线程 线程(Thread)是控制线程(Thread of Control)的缩写,是程序运行的基本单位,它是具有一定顺序的指令序列(即所编写的程序代码).存放方法中定义局部变量的栈和一些共享数据 ...

  2. Latex 学习

    Latex 学习 @(05.2 Latex)[latex][基础教程] 这篇文章是关于latex基础教程.pdf的一个总结. 基本知识 基础 1. 空格 多个空格会被视为一个空格 单个回车会被视为一个 ...

  3. UESTC 424 AreYouBusy --混合背包

    混合三种背包问题. 定义:dp[i][k]表示体积为k的时候,在前i堆里拿到的最大价值. 第一类,至少选一项,dp初值全赋为负无穷,这样才能保证不会出现都不选的情况.dp[i][k] = max(dp ...

  4. Angular2中的metadata&lpar;元数据&rpar;

    @Attrubute() 从host element 中获得普通(不是@Input)属性对应的值 适用于组件嵌套或指令, 从父组件向子组件传递数据 app.component.ts import {C ...

  5. cocos2d-x lua 使用http&lpar;下载图片&comma; POST JSON&rpar;

    cocos2d-x lua 使用http(下载图片, POST JSON) version: cocos2d-x 3.6 1.使用http post json与服务器交互 require(" ...

  6. &lbrack;Locked&rsqb; Read N Characters Given Read4 &amp&semi; Read N Characters Given Read4 II - Call multiple times

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

  7. 安装grub

    安装windows后,grub不见了 先安装Neo进入Linux 两条命令搞定. 在root用户下输入: update-grub grub-install /dev/sda

  8. How to get the file in a resource folder

    In a Maven project, we may often struggle to get a certain file (e.g. json file or sql file). Here i ...

  9. 制作Windows服务项目详细攻略

    1.在windows服务下面获得根目录: string assemblyFilePath = Assembly.GetExecutingAssembly().Location; string asse ...

  10. Selective Search for Object Recognition(理解)

    0 - 背景 在目标检测任务中,我们希望输入一副图像,输出目标所在的位置以及目标的类别.最常用的算法是滑动窗口方法,但滑动窗口其实相当于穷举图像中的所有子图像,其效率低且精度也受限.该论文提出一种新的 ...

相关文章