-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path102. Using Join Linq example in c#.cs
More file actions
41 lines (35 loc) · 1.03 KB
/
102. Using Join Linq example in c#.cs
File metadata and controls
41 lines (35 loc) · 1.03 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 CustomerId { get; set; }
public string Product { 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 { CustomerId = 1, Product = "Laptop" },
new Order { CustomerId = 2, Product = "Mouse" }
};
var customerOrders = customers.Join(orders,
customer => customer.Id,
order => order.CustomerId,
(customer, order) => new { customer.Name, order.Product }).ToList();
Console.WriteLine("Customer Orders:");
customerOrders.ForEach(co => Console.WriteLine($"{co.Name} ordered {co.Product}"));
}
}