Inner Join Linq example

Probably the most common join is an inner join. The inner join is where the result is a combination of two sets where they match or join on a common property.

In Linq it is possible to join two lists of different types, as long as they have a common property/key.

This is useful when you have two different collections and you need information from both.

A basic example

To demonstrate a basic example we are going to use two lists that contain anonymous objects.

Method syntax

Here we have two lists, people and countries. The common key that we will join on is the CountryCode from the people list and the Code from the countries list. Note that the property/key names do not have to be the same, but the values to join onto do.

The join statement is used by using the join keyword followed by the on statement.

Live example: https://dotnetfiddle.net/ZgXW8S

var people = new[]
    {
        new { Name = "Vernon", Gender = "Male", CountryCode = "GB" },
        new { Name = "Carrie", Gender = "Female", CountryCode = "CA" },
        new { Name = "Joanna", Gender = "Female", CountryCode = "US" },
        new {Name = "Thomas", Gender = "Male", CountryCode = "ES" }
    };


var countries = new[]
    {
        new {CountryName = "United Kingdom", Code = "GB"},
        new {CountryName = "United States", Code = "US"},
        new {CountryName = "Canada", Code = "CA"},
        new {CountryName = "France", Code = "FR"},
        new {CountryName = "Spain", Code = "ES"}
    };


var results = people.Join(countries,  
                    arg => arg.CountryCode, // the key to join from the people list
                    arg => arg.Code,        // the key to join from the country list
                    (person, country) => new { person.Name, country.CountryName }); // the select of the join

// output:
// Vernon United Kingdom
// Carrie Canada
// Joanna United States
// Thomas Spain

Query syntax

Here we need to specify the first list with from p in people, then we use the join statement with join c in countries. Followed by this is the statement that we need to join the properties on: on p.CountryCode equals c.Code. Notice that the word equals is used here. We will not be able to use do something like this: on p.CountryCode == c.Code. That's a no-no. When I was first learning Linq I did something like that. Don't get caught out like me!

Live example: https://dotnetfiddle.net/srO27Z

var people = new[]
    {
        new { Name = "Vernon", Gender = "Male", CountryCode = "GB" },
        new { Name = "Carrie", Gender = "Female", CountryCode = "CA" },
        new { Name = "Joanna", Gender = "Female", CountryCode = "US" },
        new {Name = "Thomas", Gender = "Male", CountryCode = "ES" }
    };


var countries = new[]
    {
        new {CountryName = "United Kingdom", Code = "GB"},
        new {CountryName = "United States", Code = "US"},
        new {CountryName = "Canada", Code = "CA"},
        new {CountryName = "France", Code = "FR"},
        new {CountryName = "Spain", Code = "ES"}
    };

var results = from p in people
              join c in countries
              on p.CountryCode equals c.Code
              select new { Name = p.Name, Country = c.CountryName };

// output:
// Vernon United Kingdom
// Carrie Canada
// Joanna United States
// Thomas Spain