1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Linq101
  6. {
  7. class Grouping
  8. {
  9. /// <summary>
  10. /// This sample uses group by to partition a list of numbers by their remainder when divided by 5.
  11. /// </summary>
  12. public void Linq40()
  13. {
  14. int[] numbers = { , , , , , , , , , };
  15.  
  16. var numberGroups = from n in numbers
  17. group n by n % into g
  18. select new { Remainder = g.Key, Numbers = g };
  19.  
  20. foreach (var numberGroup in numberGroups)
  21. {
  22. Console.WriteLine("除以5余数为{0}的有:", numberGroup.Remainder);
  23. foreach (var n in numberGroup.Numbers)
  24. {
  25. Console.WriteLine(n);
  26. }
  27. }
  28. }
  29.  
  30. /// <summary>
  31. /// This sample uses group by to partition a list of words by their first letter.
  32. /// </summary>
  33. public void Linq41()
  34. {
  35. string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
  36.  
  37. var wordGroups = from w in words
  38. group w by w[] into g
  39. select new { FirstLetter = g.Key, words = g };
  40.  
  41. foreach (var wordGroup in wordGroups)
  42. {
  43. Console.WriteLine("以字母{0}开头的单词有:", wordGroup.FirstLetter);
  44. foreach (var word in wordGroup.words)
  45. {
  46. Console.WriteLine(word);
  47. }
  48. }
  49. }
  50.  
  51. /// <summary>
  52. /// This sample uses group by to partition a list of products by category.
  53. /// </summary>
  54. public void Linq42()
  55. {
  56. var products = Data.GetProductList();
  57.  
  58. var productGroups = from p in products
  59. group p by p.Category into g
  60. select new { Category = g.Key, products = g };
  61.  
  62. //ObjectDumper.Write(productGroups,1);
  63.  
  64. foreach (var productGroup in productGroups)
  65. {
  66. Console.WriteLine("分类为{0}的产品有:", productGroup.Category);
  67. foreach (var product in productGroup.products)
  68. {
  69. ObjectDumper.Write(product);
  70. }
  71. }
  72. }
  73.  
  74. /// <summary>
  75. /// This sample uses group by to partition a list of each customer's orders, first by year, and then by month.
  76. /// </summary>
  77. public void Linq43()
  78. {
  79. var customers = Data.GetCustomerList();
  80.  
  81. var customerOrderGroups = from c in customers
  82. select new
  83. {
  84. c.CompanyName,
  85. YearGroups = from o in c.Orders
  86. group o by o.OrderDate.Year into yg
  87. select new
  88. {
  89. Year = yg.Key,
  90. MonthGoups = from o in yg
  91. group o by o.OrderDate.Month into mg
  92. select new { Month = mg.Key, mg }
  93. }
  94. };
  95.  
  96. ObjectDumper.Write(customerOrderGroups, );
  97. }
  98.  
  99. /// <summary>
  100. /// This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other.
  101. /// </summary>
  102. public void Linq44()
  103. {
  104. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
  105.  
  106. var query = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer());
  107.  
  108. ObjectDumper.Write(query, );
  109. }
  110.  
  111. private class AnagramEqualityComparer : IEqualityComparer<string>
  112. {
  113. public bool Equals(string x, string y)
  114. {
  115. return getCanonicalString(x) == getCanonicalString(y);
  116. }
  117.  
  118. public int GetHashCode(string obj)
  119. {
  120. return getCanonicalString(obj).GetHashCode();
  121. }
  122.  
  123. private string getCanonicalString(string word)
  124. {
  125. char[] wordChars = word.ToCharArray();
  126. Array.Sort(wordChars);
  127. return new string(wordChars);
  128. }
  129. }
  130.  
  131. /// <summary>
  132. /// 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.
  133. /// </summary>
  134. public void Linq45()
  135. {
  136. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
  137.  
  138. var query = anagrams.GroupBy(w => w.Trim(),
  139. a => a.ToUpper(),
  140. new AnagramEqualityComparer());
  141.  
  142. ObjectDumper.Write(query, );
  143. }
  144. }
  145. }

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. 单例-b

    这个比较老了,是mrc 里面的 此例以模仿Apple官方文档的单例写出来的.但是一直有一个非常不明白的地方,就是alloc与allocWithZone:的重载中,为什么要return [[self c ...

  2. 源代码安装GIT

    参考URL:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25150840&id=4250659 若是条件允许,从源代 ...

  3. UpdateLayeredWindow是炫效果的关键

    自绘——是的,输入框每个字都自己绘制,计算行宽,行高,模拟光标闪烁,处理输入法的各种事件,以及选中,拖动等功能. 支持支持一下,实际上无句柄的,就是多行富文本编辑比较麻烦,其他的,都不复杂.很容易实现 ...

  4. PHP+Mysql-表单数据插入数据库及数据提取完整过程

    网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...

  5. Git基本操作(Windows下)

    在开始使用Git之前,我觉得是很有必要了解下Git与其他版本控制系统的差异与文件在Git中的三种状态.可以到下面这个网站看下:Git详解之一 Git起步,了解之后,可以对Git的基本操作有一个更清晰的 ...

  6. The Embarrassed Cryptographer(高精度取模+同余模定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11435   Accepted: 3040 Description The ...

  7. Rotation Lock Puzzle

    Problem Description Alice was felling into a cave. She found a strange door with a number square mat ...

  8. Jenkins 一: 环境安装以及配置

    安装JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html 选择的JDK版本和开发使用的JDK版本最好 ...

  9. 【行业干货】ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz!

    [行业干货]ASOS:外来快时尚品牌的入华战 - 行业干货 - 京东内部论坛 - Powered by Discuz! [行业干货]ASOS:外来快时尚品牌的入华战

  10. 用phpQuery像jquery一样解析html代码

    简介 如何在php中方便地解析html代码,估计是每个phper都会遇到的问题.用phpQuery就可以让php处理html代码像jQuery一样方便. 项目地址:https://code.googl ...