Grouping Operators

  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 = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 15
 16             var numberGroups = from n in numbers
 17                                group n by n % 5 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[0] 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, 3);
 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, 1);
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, 1);
143         }
144     }
145 }
时间: 2024-11-05 21:59:58

Grouping Operators的相关文章

LINQ Sample

Sample LINQ Queries: In this section, you will learn some complex LINQ queries. We will use the following Student and Standard collection for our queries. Sample Collections: IList<Student> studentList = new List<Student>() { new Student() { S

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

Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 ToLookup ToLookup is the same as GroupBy; the only difference is the execution of GroupBy is deferred whereas ToLookup execution is immediate. IList<Stude

101个LINQ例子

Restriction Operators Where - Simple 1 public void Linq1() {    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };    var lowNums =        from n in numbers        where n < 5        select n;    Console.WriteLine("Numbers < 5:");    foreac

Flume interceptor 使用注意事项

1. 在使用 Regex Filtering Interceptor的时候一个属性是excludeEvents 当它的值为true 的时候,过滤掉匹配到当前正则表达式的一行 当它的值为false的时候,就接受匹配到正则表达式的一行 Property Name Default Description type – The component type name has to be regex_filter regex ”.*” Regular expression for matching aga

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

Restriction Operators Where - Simple 1 public void Linq1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.Wr

SQL基本语法&amp;SQLite

Databases 数据库是一个数据存储区用于存储.查询和处理数据.数据库存储我们需要的数据并且开放一个和数据交互的接口.大多数科技公司使用数据库来组织数.数据库系统包括数据库管理软件与管理控制.安全和访问控制,语言与数据库接口这些内容. 首先,我们将关注SQL语言一个结构化查询语言.它是用来查询.更新和修改数据库中的数据. SQL SQL是最常见的一种数据库语言,在任何数据专业工具箱中都是一个重要的工具.虽然SQL是一种语言,但它完全不同于像Python或r 这样的语言, SQL是专门为了与数

Microsoft REST API指南

.cnblogs-markdown {width:75%;} 本文是摘自长沙.NET技术社区翻译的英文原文文档<Microsoft REST API指南 > 转为了markdown格式便于浏览,如有需要自行下载. 链接:https://pan.baidu.com/s/1yNxUC1EcZTKccioWFYTbSg 提取码:nqgg 序言 经过3个月的碎片时间的翻译和校验,由长沙.NET技术社区翻译的英文原文文档<Microsoft REST API指南>已经翻译完成,现刊载前十一章

LINQ之路14:LINQ Operators之排序和分组(Ordering and Grouping)

本篇继续LINQ Operators的介绍,这里要讨论的是LINQ中的排序和分组功能.LINQ的排序操作符有:OrderBy, OrderByDescending, ThenBy, 和ThenByDescending,他们返回input sequence的排序版本.分组操作符GroupBy把一个平展的输入sequence进行分组存放到输出sequence中. 排序/Ordering IEnumerable<TSource>→IOrderedEnumerable<TSource> O

8.4Solr API使用(Result Grouping分组查询)

转载请出自出处:http://eksliang.iteye.com/blog/2169458 一.概述 分组统计查询不同于分组统计(Facet),facet只是简单统计记录数,并不能为每组数据返回实际的数据回来,solr提供的grouping查询能够解决这一问题,也就是说,他除了能分组外,还能把每组数据返回来. 二.语法简介 参考实例一 查询参数如下: q=*:* &group=true &group.field=price 返回结果如下: Solr Grouping参数列表 参数 参数含