Linq101-Grouping Operators
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Linq101
- {
- class Grouping
- {
- /// <summary>
- /// This sample uses group by to partition a list of numbers by their remainder when divided by 5.
- /// </summary>
- public void Linq40()
- {
- int[] numbers = { , , , , , , , , , };
- var numberGroups = from n in numbers
- group n by n % into g
- select new { Remainder = g.Key, Numbers = g };
- foreach (var numberGroup in numberGroups)
- {
- Console.WriteLine("除以5余数为{0}的有:", numberGroup.Remainder);
- foreach (var n in numberGroup.Numbers)
- {
- Console.WriteLine(n);
- }
- }
- }
- /// <summary>
- /// This sample uses group by to partition a list of words by their first letter.
- /// </summary>
- public void Linq41()
- {
- string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
- var wordGroups = from w in words
- group w by w[] into g
- select new { FirstLetter = g.Key, words = g };
- foreach (var wordGroup in wordGroups)
- {
- Console.WriteLine("以字母{0}开头的单词有:", wordGroup.FirstLetter);
- foreach (var word in wordGroup.words)
- {
- Console.WriteLine(word);
- }
- }
- }
- /// <summary>
- /// This sample uses group by to partition a list of products by category.
- /// </summary>
- public void Linq42()
- {
- var products = Data.GetProductList();
- var productGroups = from p in products
- group p by p.Category into g
- select new { Category = g.Key, products = g };
- //ObjectDumper.Write(productGroups,1);
- foreach (var productGroup in productGroups)
- {
- Console.WriteLine("分类为{0}的产品有:", productGroup.Category);
- foreach (var product in productGroup.products)
- {
- ObjectDumper.Write(product);
- }
- }
- }
- /// <summary>
- /// This sample uses group by to partition a list of each customer's orders, first by year, and then by month.
- /// </summary>
- public void Linq43()
- {
- var customers = Data.GetCustomerList();
- var customerOrderGroups = from c in customers
- select new
- {
- c.CompanyName,
- YearGroups = from o in c.Orders
- group o by o.OrderDate.Year into yg
- select new
- {
- Year = yg.Key,
- MonthGoups = from o in yg
- group o by o.OrderDate.Month into mg
- select new { Month = mg.Key, mg }
- }
- };
- ObjectDumper.Write(customerOrderGroups, );
- }
- /// <summary>
- /// This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other.
- /// </summary>
- public void Linq44()
- {
- string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
- var query = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer());
- ObjectDumper.Write(query, );
- }
- private class AnagramEqualityComparer : IEqualityComparer<string>
- {
- public bool Equals(string x, string y)
- {
- return getCanonicalString(x) == getCanonicalString(y);
- }
- public int GetHashCode(string obj)
- {
- return getCanonicalString(obj).GetHashCode();
- }
- private string getCanonicalString(string word)
- {
- char[] wordChars = word.ToCharArray();
- Array.Sort(wordChars);
- return new string(wordChars);
- }
- }
- /// <summary>
- /// This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other, and then converts the results to uppercase.
- /// </summary>
- public void Linq45()
- {
- string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
- var query = anagrams.GroupBy(w => w.Trim(),
- a => a.ToUpper(),
- new AnagramEqualityComparer());
- ObjectDumper.Write(query, );
- }
- }
- }
Linq101-Grouping Operators的更多相关文章
- Linq:Grouping Operators
[Category("Grouping Operators")] [Description("This sample uses group by to partition ...
- 101个LINQ示例,包含几乎全部操作
Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...
- Linq编程101例
原文地址:101 LINQ Samples in C# Part1 - Restriction Operators Part2 - Projection Operators Part3 - Parti ...
- Flume interceptor 使用注意事项
1. 在使用 Regex Filtering Interceptor的时候一个属性是excludeEvents 当它的值为true 的时候,过滤掉匹配到当前正则表达式的一行 当它的值为false的时候 ...
- 【翻译】Flume 1.8.0 User Guide(用户指南) Processors
翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...
- tfs二次开发-利用tfs api做查询
参考地址:https://msdn.microsoft.com/en-us/library/bb130306(v=vs.120).aspx You can query for bugs, tasks, ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- matlab 正则表达式
regexprep Replace text using regular expression collapse all in page Syntax newStr = regexprep(str,e ...
- flume1.9 用户指南(中文版)
概述 Apache Flume是一个分布式,可靠且可用的系统,用于有效地从许多不同的source收集,聚合和移动大量日志数据到集中式数据存储. Apache Flume的使用不仅限于日志数据聚合.由于 ...
- 一次flume exec source采集日志到kafka因为单条日志数据非常大同步失败的踩坑带来的思考
本次遇到的问题描述,日志采集同步时,当单条日志(日志文件中一行日志)超过2M大小,数据无法采集同步到kafka,分析后,共踩到如下几个坑.1.flume采集时,通过shell+EXEC(tail -F ...
随机推荐
- 单例-b
这个比较老了,是mrc 里面的 此例以模仿Apple官方文档的单例写出来的.但是一直有一个非常不明白的地方,就是alloc与allocWithZone:的重载中,为什么要return [[self c ...
- 源代码安装GIT
参考URL:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25150840&id=4250659 若是条件允许,从源代 ...
- UpdateLayeredWindow是炫效果的关键
自绘——是的,输入框每个字都自己绘制,计算行宽,行高,模拟光标闪烁,处理输入法的各种事件,以及选中,拖动等功能. 支持支持一下,实际上无句柄的,就是多行富文本编辑比较麻烦,其他的,都不复杂.很容易实现 ...
- PHP+Mysql-表单数据插入数据库及数据提取完整过程
网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...
- Git基本操作(Windows下)
在开始使用Git之前,我觉得是很有必要了解下Git与其他版本控制系统的差异与文件在Git中的三种状态.可以到下面这个网站看下:Git详解之一 Git起步,了解之后,可以对Git的基本操作有一个更清晰的 ...
- The Embarrassed Cryptographer(高精度取模+同余模定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11435 Accepted: 3040 Description The ...
- Rotation Lock Puzzle
Problem Description Alice was felling into a cave. She found a strange door with a number square mat ...
- Jenkins 一: 环境安装以及配置
安装JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 选择的JDK版本和开发使用的JDK版本最好 ...
- 【行业干货】ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz!
[行业干货]ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz! [行业干货]ASOS:外来快时尚品牌的入华战
- 用phpQuery像jquery一样解析html代码
简介 如何在php中方便地解析html代码,估计是每个phper都会遇到的问题.用phpQuery就可以让php处理html代码像jQuery一样方便. 项目地址:https://code.googl ...