Coder Perfect

Linq-to-Entities GroupJoin vs. Join


I’m not sure what a GroupJoin() is.

What makes it unique from a standard Join()?

Is it a widely used term?

Is it only applicable to method syntax? What about the syntax of the query? (An example of C# code would be helpful.)

Asked by duyn9uyen

Solution #1

Let’s pretend you have two lists:

Id  Value
1   A
2   B
3   C

Id  ChildValue
1   a1
1   a2
1   a3
2   b1
2   b2

When the two lists are joined on the Id field, the following is the result:

Value ChildValue
A     a1
A     a2
A     a3
B     b1
B     b2

The outcome of GroupJoining the two lists on the Id field is:

Value  ChildValues
A      [a1, a2, a3]
B      [b1, b2]
C      []

As a result, Join returns a flat (tabular) result containing both parent and child values. In the first list, GroupJoin creates a list of entries, each with a group of joined entries in the second list.

Because there are no entries for C, Join is the SQL equivalent of INNER JOIN. While GroupJoin is the OUTER JOIN equivalent: C appears in the result set, but with an empty list of related entries (in an SQL result set there would be a row C – null).

So let’s call the two lists IEnumerableParent> and IEnumerableChild>. (IQueryableT> in the instance of Linq to Entities).

The syntax for joining would be

from p in Parent
join c in Child on p.Id equals c.Id
select new { p.Value, c.ChildValue }

IEnumerableX> is returned, where X is an anonymous type with two properties: Value and ChildValue. Under the hood, the Join technique is used in this query syntax.

The syntax for GroupJoin would be

from p in Parent
join c in Child on p.Id equals c.Id into g
select new { Parent = p, Children = g }

returning an IEnumerableY>, where Y is an anonymous type that has one Parent property and one IEnumerableChild> property. The GroupJoin technique is used behind the scenes in this query syntax.

In the latter query, we might just do select g, which would choose an IEnumerableIEnumerableChild>>, such as a list of lists. In many circumstances, the choose that includes the parent is more useful.

As previously stated, the statement…

from p in Parent
join c in Child on p.Id equals c.Id into g
select new { Parent = p, Children = g }

… returns a list of parents and their children. Two tiny additions make this into a flat list of parent-child pairs:

from p in parents
join c in children on p.Id equals c.Id into g // <= into
from c in g.DefaultIfEmpty()               // <= flattens the groups
select new { Parent = p.Value, Child = c?.ChildValue }

This is the end outcome.

Value Child
A     a1
A     a2
A     a3
B     b1
B     b2
C     (null)

In the above line, the range variable c is reused. By adding the equivalent of into g from c in g, any join statement may be easily changed to an outer join. DefaultIfEmpty() to a join statement that already exists.

The query (or comprehensive) syntax shines in this situation. Method (or fluent) syntax reflects reality, although it’s difficult to write:

parents.GroupJoin(children, p => p.Id, c => c.Id, (p, c) => new { p, c })
       .SelectMany(x => x.c.DefaultIfEmpty(), (x,c) => new { x.p.Value, c?.ChildValue } )

In LINQ, a flat outer join is a GroupJoin that has been flattened via SelectMany.

Let’s pretend that the list of parents is a little longer. Some user interfaces generate a list of selected parents as Id values in a predetermined order. Let’s put it this way:

var ids = new[] { 3,7,2,4 };

The selected parents must now be filtered in this exact order from the parents list.

If this is the case,

var result = parents.Where(p => ids.Contains(p.Id));

The result will be determined by the sequence in which the parents are born. Parents 2, 3, 4, and 7 will be the result if the parents are arranged by Id. This isn’t good. We can, however, use join to filter the list. The order will be retained by using ids as the first list:

from id in ids
join p in parents on id equals p.Id
select p

The result is parents 3, 7, 2, 4.

Answered by Gert Arnold

Solution #2

According to eduLINQ:

The return statement is the only distinction:


var lookup = inner.ToLookup(innerKeySelector, comparer); 
foreach (var outerElement in outer) 
    var key = outerKeySelector(outerElement); 
    foreach (var innerElement in lookup[key]) 
        yield return resultSelector(outerElement, innerElement); 


var lookup = inner.ToLookup(innerKeySelector, comparer); 
foreach (var outerElement in outer) 
    var key = outerKeySelector(outerElement); 
    yield return resultSelector(outerElement, lookup[key]); 

Read more here:

Answered by MarcinJuraszek

Solution #3

Assume you’ve got two separate classes:

public class Person
    public string Name, Email;

    public Person(string name, string email)
        Name = name;
        Email = email;
class Data
    public string Mail, SlackId;

    public Data(string mail, string slackId)
        Mail = mail;
        SlackId = slackId;

Now, let’s get ready to work with the data:

var people = new Person[]
        new Person("Sudi", ""),
        new Person("Simba", ""),
        new Person("Sarah", string.Empty)

    var records = new Data[]
        new Data("", "Sudi_Try"),
        new Data("", "Sudi@Test"),
        new Data("", "SimbaLion")

You’ll notice that sudi@try is capitalized. There are two slackIds on disc. That was created to demonstrate how Join works.

Let’s put up a query to connect Person and Data:

var query = people.Join(records,
        x => x.Email,
        y => y.Mail,
        (person, record) => new { Name = person.Name, SlackId = record.SlackId});

You could also use a foreach to traverse over the query after it’s been built:

foreach (var item in query)
        Console.WriteLine($"{item.Name} has Slack ID {item.SlackId}");

Let’s also print the GroupJoin result:


            x => x.Email,
            y => y.Mail,
            (person, recs) => new {
                Name = person.Name,
                SlackIds = recs.Select(r => r.SlackId).ToArray() // You could materialize //whatever way you want.

You will notice that the GroupJoin will put all SlackIds in a single group.

Answered by Sudi Dav

Post is based on