How to use LINQ to join two lists in C#? - Biz Tech

How to use LINQ to join two lists in C#?

Listen
public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public int Age { get; set; }
}

public class Address
{
    public string City { get; set; }
    public string State { get; set; }
}

List<Person> people = new List<Person>
{
    new Person { Name = "John", Gender = "Male", Age = 30 },
    new Person { Name = "Jane", Gender = "Female", Age = 25 },
    new Person { Name = "Bob", Gender = "Male", Age = 35 }
};

List<Address> addresses = new List<Address>
{
    new Address { City = "New York", State = "NY" },
    new Address { City = "Los Angeles", State = "CA" }
};

var joinedList = from person in people
                 join address in addresses on person.Name equals address.City
                 select new { Name = person.Name, City = address.City, State = address.State };

foreach (var item in joinedList)
{
    Console.WriteLine(item.Name + " - " + item.City + ", " + item.State);
}