-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGrouping.cs
91 lines (80 loc) · 2.92 KB
/
Grouping.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
namespace StandardOperators;
public static class Grouping
{
public static void GroupingQueryExpression()
{
IEnumerable<IGrouping<string, Course>> subjectGroups =
from course in Course.Catalog
group course by course.Category;
foreach (IGrouping<string, Course> group in subjectGroups)
{
Console.WriteLine($"Category: {group.Key}");
Console.WriteLine();
foreach (Course course in group)
{
Console.WriteLine(course.Title);
}
Console.WriteLine();
}
}
public static void ExpandingSimpleGroupingQuery()
{
IEnumerable<IGrouping<string, Course>> subjectGroups =
Course.Catalog.GroupBy(course => course.Category);
}
public static void GroupingQueryItemProjection()
{
IEnumerable<IGrouping<string, string>> subjectGroups =
from course in Course.Catalog
group course.Title by course.Category;
}
public static void ExpandedGroupingQueryItemProjection()
{
IEnumerable<IGrouping<string, string>> subjectGroups = Course.Catalog
.GroupBy(course => course.Category, course => course.Title);
}
public static void GroupingQueryGroupProjection()
{
IEnumerable<string> subjectGroups =
from course in Course.Catalog
group course by course.Category into category
select $"Category '{category.Key}' contains {category.Count()} courses";
}
public static void ExpandedGroupingQueryGroupProjection()
{
IEnumerable<string> subjectGroups = Course.Catalog
.GroupBy(course => course.Category)
.Select(category =>
$"Category '{category.Key}' contains {category.Count()} courses");
}
public static void GroupByWithKeyAndGroupProjection()
{
IEnumerable<string> subjectGroups = Course.Catalog.GroupBy(
course => course.Category,
(category, courses) =>
$"Category '{category}' contains {courses.Count()} courses");
}
public static void GroupByWithKeyItemAndGroupProjection()
{
IEnumerable<string> subjectGroups = Course.Catalog.GroupBy(
course => course.Category,
course => course.Title,
(category, titles) =>
$"Category '{category}' contains {titles.Count()} courses: " +
string.Join(", ", titles));
}
public static void CompositeGroupKey()
{
var bySubjectAndYear =
from course in Course.Catalog
group course by new { course.Category, course.PublicationDate.Year };
foreach (var group in bySubjectAndYear)
{
Console.WriteLine($"{group.Key.Category} ({group.Key.Year})");
foreach (Course course in group)
{
Console.WriteLine(course.Title);
}
}
}
}