-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
GroupBy-Nested-Sample-1.cs
55 lines (52 loc) · 2.28 KB
/
GroupBy-Nested-Sample-1.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Grouping
{
public class GroupByNested1
{
//This sample uses group by to partition a list of each customer's orders,
// first by year, and then by month.
public static void QuerySyntaxExample()
{
List<Customer> customers = Data.Customers;
var customerOrderGroups = from c in customers
select new
{
c.CustomerName,
YearGroups =
from o in c.Orders
group o by o.OrderDate.Year into yg
select new
{
Year = yg.Key,
MonthGroups =
from o in yg
group o by o.OrderDate.Month into mg
select new
{
Month = mg.Key,
Orders = mg
}
}
};
foreach (var c in customerOrderGroups)
{
Console.WriteLine("-------------------");
Console.WriteLine($"CustomerName={c.CustomerName}");
foreach (var yg in c.YearGroups)
{
Console.WriteLine($"Year={yg.Year}");
foreach (var mg in yg.MonthGroups)
{
Console.WriteLine($"Month={mg.Month}");
foreach (var o in mg.Orders)
{
Console.WriteLine($"OrderDate={o.OrderDate}, Total={o.Total}");
}
}
}
}
}
}
}