forked from rogerwcpt/python-linq-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
163 lines (141 loc) · 5.69 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using linqshared;
namespace linq_grouping
{
class Program: ProgramBase
{
static void Main(string[] args)
{
Linq40();
// Linq41();
// Linq42();
// Linq43();
// Linq44();
// Linq45();
}
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)
{
var wordChars = word.ToCharArray();
Array.Sort<char>(wordChars);
return new string(wordChars);
}
}
[Category("Grouping Operators")]
[Description("This sample uses grouping to partition a list of numbers by their remainder when divided by 5.")]
static void Linq40()
{
var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numberGroups = numbers
.GroupBy(n => n % 5)
.Select(x =>
new
{
Remainder = x.Key,
Numbers = x
});
numberGroups.ForEach((g) =>
{
Console.WriteLine("Numbers with a remainder of {0} when divided by 5:", g.Remainder);
g.Numbers.ForEach(Console.WriteLine);
});
}
[Category("Grouping Operators")]
[Description("This sample uses grouping to partition a list of words by their first letter.")]
static void Linq41()
{
var words = new [] { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
var wordGroups = words
.GroupBy(w => w[0])
.Select(g =>
new
{
FirstLetter = g.Key,
Words = g
});
wordGroups.ForEach((g) =>
{
Console.WriteLine($"Words that start with the letter '{g.FirstLetter}':");
g.Words.ForEach(Console.WriteLine);
});
}
[Category("Grouping Operators")]
[Description("This sample uses grouping to partition a list of products by category.")]
static void Linq42()
{
var products = GetProductList();
var orderGroups = products
.GroupBy(p => p.Category)
.Select(g =>
new
{
Category = g.Key,
Products = g
});
ObjectDumper.Write(orderGroups, 1);
}
[Category("Grouping Operators")]
[Description("This sample uses nested grouping to partition a list of each customer's orders, first by year, and then by month.")]
static void Linq43()
{
var customers = GetCustomerList();
var customerOrderGroups = customers
.Select(c => new
{
c.CompanyName,
YearGroups =
(
c.Orders
.GroupBy(y => y.OrderDate.Year)
.Select(YearGroup => new
{
Year = YearGroup.Key,
MonthGroups =
(
YearGroup
.GroupBy(m => m.OrderDate.Month)
.Select(MonthGroup => new
{
Month = MonthGroup.Key, Orders = MonthGroup
})
)
})
)
});
ObjectDumper.Write(customerOrderGroups, 3);
}
[Category("Grouping Operators")]
[Description("This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other.")]
static void Linq44()
{
var anagrams = new [] { "from ", " salt", " earn ", " last ", " near ", " form " };
var orderGroups = anagrams
.GroupBy(w => w.Trim(), new AnagramEqualityComparer());
ObjectDumper.Write(orderGroups, 1);
}
[Category("Grouping Operators")]
[Description("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.")]
static void Linq45()
{
var anagrams = new [] { "from ", " salt", " earn ", " last ", " near ", " form " };
var orderGroups = anagrams.GroupBy(
w => w.Trim(),
a => a.ToUpper(),
new AnagramEqualityComparer()
);
ObjectDumper.Write(orderGroups, 1);
}
}
}