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 ...
随机推荐
- 跨进程发送消息数据(发送WM_COPYDATA消息,够简单的)
1 //1.发送窗体 2 procedure TForm2.Button1Click(Sender: TObject); 3 var 4 h: HWND; 5 Size: Integer; 6 Cop ...
- mysql存储过程写法—动态参数运用
--删除 双击代码全选 1 drop procedure if exists up_common_select --创建 双击代码全选 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 【转】 linux iio子系统
原文网址:http://blog.csdn.net/tsy20100200/article/details/47101661 最近由于工作的需要,接触了Linux iio子系统,对于这个目录其实以前是 ...
- AC自动机(Aho-Corasick automation)模板 HDU:2222
#include <iostream> #include <cstdio> #include <cstring> #include <queue> us ...
- wchar_t与char、wstring与string的相互转换
个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...
- iOS开发ARC入门和使用
本文引自:http://www.onevcat.com/2012/06/arc-hand-by-hand/ 英文原版:http://www.raywenderlich.com/5677/beginni ...
- bzoj 1191 [HNOI2006]超级英雄Hero(最大基数匹配)
1191: [HNOI2006]超级英雄Hero Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2813 Solved: 1331[Submit][ ...
- 《University Calculus》-chaper8-无穷序列和无穷级数-比值审敛法
在分析等比级数的过程中,我们发现对于q<1的等比级数是收敛的,它表示级数每一项与它前一项的比值小于1,我们能否将这种方法推广起来用于一般级数的审敛呢? 从极限的定义出发:
- Mac下Intellij IDea发布Web项目详解一
Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intellij IDea发布J ...
- POJ3617 Best Cow Line
其实是学习参考了算法书的代码,但仍然是我自己写的,有小差别.贪心类型. #include <iostream> using namespace std; int main() { int ...