-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8. Group Join Linq example in c#.cs
More file actions
41 lines (35 loc) · 1.08 KB
/
8. Group Join Linq example in c#.cs
File metadata and controls
41 lines (35 loc) · 1.08 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; }
}
static void Main()
{
var customers = new List<Customer>
{
new Customer { Id = 1, Name = "Alice" },
new Customer { Id = 2, Name = "Bob" }
};
var orders = new List<Order>
{
new Order { Id = 1, CustomerId = 1 },
new Order { Id = 2, CustomerId = 1 },
new Order { Id = 3, CustomerId = 2 }
};
var groupedOrders = from c in customers
join o in orders on c.Id equals o.CustomerId into orderGroup
select new { Customer = c.Name, Orders = orderGroup.Count() };
Console.WriteLine("Customer Orders Count:");
groupedOrders.ToList().ForEach(g => Console.WriteLine($"{g.Customer}: {g.Orders}"));
}
}