LINQ basic group (left outer) join

The left outer join is my favourite join, so it’s worth giving it a post of its own…
VB.NET

Dim groupJoinResults = From instA In Me.LINQ2ClassAList _
                       Group Join instB In Me.LINQ2ClassBList _
                       On instA.Region Equals instB.Region _
                       Into groupedBs = Group _
                       Select instA, groupedBs

C#

var groupJoinResults = from instA in this.LINQ2ClassAList
                       join instB in this.LINQ2ClassBList
                       on instA.Region equals instB.Region
                       into groupedBs
                       select new { instA, groupedBs };

In this example the groupedBs are accessible via (result).groupedBs and are themselves var/anonymous types.
groupJoinResults.First().instA.Name = the Name of the first instA returned and
groupJoinResults.First().groupedBs.First().Name = the Name of the first instB that matched it.

In my words a Group Join is a Left Outer Join with the results of the right dumped into an IEnumerable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s