-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7. Join Two Collections Linq example in c#.cs
More file actions
41 lines (35 loc) · 1.08 KB
/
7. Join Two Collections 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; }
public DateTime OrderDate { 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, OrderDate = DateTime.Now },
new Order { Id = 2, CustomerId = 2, OrderDate = DateTime.Now }
};
var customerOrders = from c in customers
join o in orders on c.Id equals o.CustomerId
select new { c.Name, o.OrderDate };
Console.WriteLine("Customer Orders:");
customerOrders.ToList().ForEach(co => Console.WriteLine($"{co.Name}: {co.OrderDate}"));
}
}