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 ...
随机推荐
- Java高阶面试问题合集
下面总结一下在Java面试中常用的一些问题,不具体解答,我只附上一些精彩的博文链接. Spring IOC AOP 底层原理 JAVA的反射机制和动态代理 Java反射机制和动态代理 多线程 Spri ...
- Hybrid UI framework shootout: Ionic vs. Famo.us vs. F7 vs. OnsenUI
1 Introduction In the past 2 years I’ve been working intensively on mobile applications, mostly hybr ...
- jQuery Asynchronous
http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html http:// ...
- 自己动手实现简单的Vector
看到今天,终于自己动手写了一个自己的vector,我这个版本的vector只有vector主要的一些操作,包括原版vector的所有构造函数,begin(),end(),size(),capacity ...
- HDOJ 2016 数据的交换输出
Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数. Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测 ...
- 【Android Studio】No JVM installation found
如果没有配置好JDK的环境变量,启动Android Studio的时候会报错: 请参考我整理的博客文章<JDK的下载.安装和配置>,链接:http://www.cnblogs.com/du ...
- 关于Linux
这是一个2B让我写的关于Linux的一点东西. 其实我对Linux一直都是持有一种很尊敬的态度,作为一个非商业性的操作系统,能够成长成这样简直是不可思议,有一种Dota在游戏界的感觉,很让人佩服.但是 ...
- 安装 macbook 双系统( OS X 和 Ubuntu )
打算 macbook 上面多安装一个 ubuntu 系统来用下.流程大致下面几步: 1. 备份重要资料 2. 划分硬盘区域用于安装 ubuntu 3. 下载 ubuntu ISO 文件,并刻录到 U ...
- Spring 配置中的 default-lazy-init属性
spring的容器是提供了lazy-load的,即默认的缺省设置是bean没有lazy-load,该属性处于false状态,这样导致spring在启动过程导致在启动时候,会默认加载整个对象实例图,从初 ...
- 【转】JavaScript对Json节点的增删改
var json = { "age":24, "name":"cst" }; //修改Json中的age值,因为Json中存在age属性 j ...