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 ...
随机推荐
- bzoj2734: [HNOI2012]集合选数
Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中 ...
- 转:gpio_request
今天再次学习SD卡驱动,遇到pgio_request这个函数,始终不知道其什么意思,看了几遍源代码才有了点感觉.现将其关键部分再此说明一下,以备自己以后复习,或是路客参考. 一般gpio_reques ...
- C# WinForm的SplitContainer控件固定Panel大小[转]
原文地址:http://zhidao.baidu.com/link?url=mhkUszZ8am_vqNX3KAOff-psd3af7Xl3DL77KxJ-rWIAqIArQHzQIEBoX49mQA ...
- oracle timestamp的转换
select to_char(stime,'yyyy-mm-dd HH24:MI:ss.ff3') from e_bmp_log_operation t where t.sdetail like '% ...
- Yaroslav and Divisors
Codeforces Round #182 (Div. 1) D:http://codeforces.com/contest/301/problem/D 题意:给一个1-n,n个数的序列,然后查询一个 ...
- George and Cards
Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E 题意:给你一个n个数的序列,然后给你一个标准序 ...
- Summary Ranges —— LeetCode
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 第一个ios程序
1.ios的理解: Operating System,简称OS,操作系统,ios是苹果操作系统. 2.Xcode开发环境: 苹果公司开发的编程软件,是开发人员建立OS X 和 iOS 应用程序的最快捷 ...
- cf703B Mishka and trip
B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [Java] JavaMail 简单案例
网易提供了免费的 SMTP / POP3服务,可用于编程测试,详情见 什么是POP3.SMTP和IMAP? 只需要拥有一个网易邮箱账号,并开启该账号的 SMTP / POP3 功能,便可以通过程序发送 ...