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的更多相关文章

  1. Linq:Grouping Operators

    [Category("Grouping Operators")] [Description("This sample uses group by to partition ...

  2. 101个LINQ示例,包含几乎全部操作

    Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...

  3. Linq编程101例

    原文地址:101 LINQ Samples in C# Part1 - Restriction Operators Part2 - Projection Operators Part3 - Parti ...

  4. Flume interceptor 使用注意事项

    1. 在使用 Regex Filtering Interceptor的时候一个属性是excludeEvents 当它的值为true 的时候,过滤掉匹配到当前正则表达式的一行 当它的值为false的时候 ...

  5. 【翻译】Flume 1.8.0 User Guide(用户指南) Processors

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  6. tfs二次开发-利用tfs api做查询

    参考地址:https://msdn.microsoft.com/en-us/library/bb130306(v=vs.120).aspx You can query for bugs, tasks, ...

  7. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  8. matlab 正则表达式

    regexprep Replace text using regular expression collapse all in page Syntax newStr = regexprep(str,e ...

  9. flume1.9 用户指南(中文版)

    概述 Apache Flume是一个分布式,可靠且可用的系统,用于有效地从许多不同的source收集,聚合和移动大量日志数据到集中式数据存储. Apache Flume的使用不仅限于日志数据聚合.由于 ...

  10. 一次flume exec source采集日志到kafka因为单条日志数据非常大同步失败的踩坑带来的思考

    本次遇到的问题描述,日志采集同步时,当单条日志(日志文件中一行日志)超过2M大小,数据无法采集同步到kafka,分析后,共踩到如下几个坑.1.flume采集时,通过shell+EXEC(tail -F ...

随机推荐

  1. BZOJ 3196 二逼平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的 ...

  2. c# 绘图常用对象和方法

    //BitMap 位图,常用的方法,     Save:主要方式有:(1)保存在图像文件里,可以指定格式[gif,bmp]:(2) 保存在流中,以指定格式[gif,bmp]         //gra ...

  3. 今天愉快的hack小记

    今天发生了一件很好玩的事情...那就是WZJ的数据结构(负五)被人水掉了...用的是线段树暴力大发好... XYZ折腾了多长时间的论文题就这么被搞掉了...?窝来维护正义了! 怎么卡呢:让线段树走到叶 ...

  4. Linux Shell编程(8)——变量详解

    不同与许多其他的编程语言,Bash不以"类型"来区分变量.本质上来说,Bash变量是字符串,但是根据环境的不同,Bash允许变量有整数计算和比较.其中的决定因素是变量的值是不是只含 ...

  5. Ganglia监控搭建

    一.Ganglia介绍: Ganglia是一个监控服务器.集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标.Ga ...

  6. SVN服务器搭建(与apache整合)

    一.SVN介绍 SVN是一个版本控制工具,Subversion的版本库(repository),就是位于服务器,统一管理和储存数据的地方. 二.SVN数据存储方式 在Subversion中,版本库的数 ...

  7. 数学(快速数论变换):SDOI2015 序列统计

    [题目描述] 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S. 小C用这个生成器生成了许多这样的数列.但是小C有 ...

  8. 解决mysql不能远程登录的问题

    1. 改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...

  9. UITextField知多少

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  10. STRUCTS 2 UPLOAD

    {LJ?Dragon}[标题]structs2 上传文件中文乱码问题 {LJ?Dragon}[Daily] 1.配置struts.xml文件 <?xml version="1.0&qu ...