Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

GroupJoin Operator

Modified: 2008/07/23 05:21 by yurik - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The GroupJoin operator performs a grouped join of two sequences based on matching keys extracted from the elements.

Edit

Method Signatures

// 1 - Group Join
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
    this IEnumerable<TOuter> outer, 
    IEnumerable<TInner> inner, 
    Func<TOuter, TKey> outerKeySelector, 
    Func<TInner, TKey> innerKeySelector, 
    Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
 
// New in January 2007 CTP
// 2 - Group Join using Custom Comparer
public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
    this IEnumerable<TOuter> outer, 
    IEnumerable<TInner> inner, 
    Func<TOuter, TKey> outerKeySelector, 
    Func<TInner, TKey> innerKeySelector, 
    Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, 
    IEqualityComparer<TKey> comparer)



Edit

Exceptions

Throws an ArgumentNullException if outer, inner, outerKeySelector, innerKeySelector or resultCollector is null.

Edit

Pseudo-code

TODO: Needs pseudo-code explanation.

Edit

Loop count

1. A complete loop of the inner sequence builds key groups. Groups returned by iteration pattern. This operator implements the standard deferred execution iterator pattern. This means, no looping will occur until the result is iterated over.

Edit

Code Samples

In this example, var q use the Group Join Query Expression syntax (notice the into, a cross-join doesn't have this), and var q1 implements the same code using a sub-query in the select projection. They return identical results.

public static void GroupJoinSimpleExample()
{
    var customers = new List<Customer>() { 
        new Customer {Key = 1, Name = "Gottshall" },
        new Customer {Key = 2, Name = "Valdes" },
        new Customer {Key = 3, Name = "Gauwain" },
        new Customer {Key = 4, Name = "Deane" },
        new Customer {Key = 5, Name = "Zeeman" } 
    };
 
    var orders = new List<Order>() {
        new Order {Key = 1, OrderNumber = "Order 1" },
        new Order {Key = 1, OrderNumber = "Order 2" },
        new Order {Key = 4, OrderNumber = "Order 3" },
        new Order {Key = 4, OrderNumber = "Order 4" },
        new Order {Key = 5, OrderNumber = "Order 5" }
    };
 
    var q = from c in customers
            join o in orders on c.Key equals o.Key into g
            select new {CustomerName = c.Name, Orders = g};
 
    var q1 = from c in customers
             select new { CustomerName = c.Name, 
                          Orders = from o in orders
                                   where c.Key == o.Key
                                   select o };
 
    foreach (var group in q1) {
        Console.WriteLine("Customer: {0}", group.CustomerName);
        foreach (var order in group.Orders)
            Console.WriteLine("  - {0}", order.OrderNumber);
    }
 
    Console.ReadLine();
}
 
public class Customer
{
     public int Key;
     public string Name;
}
 
public class Order
{
    public int Key;
    public string OrderNumber;
}
 



Customer: Gottshall
  - Order 1
  - Order 2
Customer: Valdes
Customer: Gauwain
Customer: Deane
  - Order 3
  - Order 4
Customer: Zeeman
  - Order 5

Edit

VB Sample

In VB Group Join operator performs a grouped join of two collections based on matching keys extracted from the elements.

Public Sub Linq()
 
 Dim procNames() As String = {"svc", "devenv", "winlogon", "explorer"}
 
  Dim query = From process In procNames _
              Group Join procs In System.Diagnostics.Process.GetProcesses() _
              On procs.ProcessName Equals process _
              Into Count()
 
        Console.WriteLine("Result:")
        For Each c In query
            Console.WriteLine(c.Count)
        Next
End Sub



Result may vary,
Result:
0
2
2
13
1

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.