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 }