I have web searched but I still cant find a simple answer. Can someone please explain (in simple English) what a GroupJoin
is? How is it different from a regular inner Join
? Is it commonly used? Is it only for method syntax? What about query syntax? A c# code example would be nice.
This question is related to
c#
linq
linq-to-entities
Let's suppose you have two different 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 Prepare data to work with:
var people = new Person[]
{
new Person("Sudi", "[email protected]"),
new Person("Simba", "[email protected]"),
new Person("Sarah", string.Empty)
};
var records = new Data[]
{
new Data("[email protected]", "Sudi_Try"),
new Data("[email protected]", "Sudi@Test"),
new Data("[email protected]", "SimbaLion")
};
You will note that [email protected] has got two slackIds. I have made that for demonstrating how Join works.
Let's now construct the query to join Person with Data:
var query = people.Join(records,
x => x.Email,
y => y.Mail,
(person, record) => new { Name = person.Name, SlackId = record.SlackId});
Console.WriteLine(query);
After constructing the query, you could also iterate over it with a foreach like so:
foreach (var item in query)
{
Console.WriteLine($"{item.Name} has Slack ID {item.SlackId}");
}
Let's also output the result for GroupJoin:
Console.WriteLine(
people.GroupJoin(
records,
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.
According to eduLINQ:
The best way to get to grips with what GroupJoin does is to think of Join. There, the overall idea was that we looked through the "outer" input sequence, found all the matching items from the "inner" sequence (based on a key projection on each sequence) and then yielded pairs of matching elements. GroupJoin is similar, except that instead of yielding pairs of elements, it yields a single result for each "outer" item based on that item and the sequence of matching "inner" items.
The only difference is in return statement:
Join:
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);
}
}
GroupJoin:
var lookup = inner.ToLookup(innerKeySelector, comparer);
foreach (var outerElement in outer)
{
var key = outerKeySelector(outerElement);
yield return resultSelector(outerElement, lookup[key]);
}
Read more here:
Source: Stackoverflow.com