GroupJoin Linq example

The GroupJoin is similar to a Join, but with a little twist of Grouping. The best way to explain this is an example.

A basic example

Similar to the inner join example, we are going to use two lists that contain anonymous objects which we would like to join as well as group by a certain property.

Method syntax

Let's say we have a list of people along with which country they reside in, and we have another separate list of countries.

How can we find out or group people by country and join them together? Well, in this situation, we can use a GroupJoin

The result will be a list of countries and within each country will be a list of people that belong to that country.

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

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" },
        new { Name = "James", Gender = "Male", CountryCode = "GB" },
        new { Name = "Elly", Gender = "Female", CountryCode = "US" }
    };

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 = countries.GroupJoin(people, 
                        arg => arg.Code, // the key to select from countries
                        arg => arg.CountryCode, // the key to select from people
                        (country, p) => new { country, PeopleInCountry = p }); // the selection
            
// output:
// United Kingdom
//   Vernon
//   James
// United States
//   Joanna
//   Elly
// Canada
//   Carrie
// France
// Spain
//   Thomas

Query syntax

For the query syntax, we need to perform a join followed by a group by which is where the into comes in.

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

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" },
        new { Name = "James", Gender = "Male", CountryCode = "GB" },
        new { Name = "Elly", Gender = "Female", CountryCode = "US" }
    };

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 c in countries
              join p in people
              on c.Code equals p.CountryCode 
              into g
              select new { Country = c, PeopleInCountry = g};

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