TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# GroupJoin Method

This C# program demonstrates the GroupJoin extension method. It requires System.Linq.

GroupJoin is an extension method.

It groups one collection of objects by a key and joins those groups with another collection of keyed objects. With this method, we create a collection where, at each key, a group of results is placed.

Example. The GroupJoin method is challenging to correctly use. It requires at least four arguments. We can specify these Func types with the lambda expression syntax. Here we review these four arguments.

Func

Arguments 1 and 2: Argument one is the secondary collection. And argument two is a Func that returns the key from the first object type.

Arguments 3 and 4: A Func that returns the key from the second object type, and one that stores the grouped object with the group itself.

In this example, we use regular classes. It is probably more common to use anonymous types with GroupJoin. We can finally loop over the resulting collection with the foreach loop construct.

Lambda ExpressionForeach

C# program that uses GroupJoin method

using System;
using System.Collections.Generic;
using System.Linq;

class Customer
{
    public int Code { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int KeyCode { get; set; }
    public string Product { get; set; }
}

class Result
{
    public string Name { get; set; }
    public IEnumerable<Order> Collection { get; set; }
    public Result(string name, IEnumerable<Order> collection)
    {
	this.Name = name;
	this.Collection = collection;
    }
}

class Program
{
    static void Main()
    {
	// Example customers.
	var customers = new Customer[]
	{
	    new Customer{Code = 5, Name = "Sam"},
	    new Customer{Code = 6, Name = "Dave"},
	    new Customer{Code = 7, Name = "Julia"},
	    new Customer{Code = 8, Name = "Sue"}
	};

	// Example orders.
	var orders = new Order[]
	{
	    new Order{KeyCode = 5, Product = "Book"},
	    new Order{KeyCode = 6, Product = "Game"},
	    new Order{KeyCode = 7, Product = "Computer"},
	    new Order{KeyCode = 7, Product = "Mouse"},
	    new Order{KeyCode = 8, Product = "Shirt"},
	    new Order{KeyCode = 5, Product = "Underwear"}
	};

	// Correlate "customers" with "orders"
	// ... Use Code property as key for Customer.
	// ... Use KeyCode property as key for Order.
	// ... For each result, create object with Name and IEnumerable of orders.
	var query = customers.GroupJoin(orders,
	    c => c.Code,
	    o => o.KeyCode,
	    (c, result) => new Result(c.Name, result));

	// Enumerate results.
	foreach (var result in query)
	{
	    Console.WriteLine("{0} bought...", result.Name);
	    foreach (var item in result.Collection)
	    {
		Console.WriteLine(item.Product);
	    }
	}
    }
}

Output

Sam bought...
Book
Underwear
Dave bought...
Game
Julia bought...
Computer
Mouse
Sue bought...
Shirt

Discussion. The GroupJoin method is probably more effort to use than it is worth. It would be possible to create a similar implementation using an object model. You could use a Dictionary, put objects as the keys, while implementing IEqualityComparer.

DictionaryIEqualityComparer

Next, you could use the List type to store the individual objects on each value in the Dictionary. Methods could be added to find those objects or determine their existence. This solution would be imperative in nature.

However: This solution would have the performance advantages of Dictionary, while avoiding any performance drawbacks of GroupJoin.

List

Summary. With GroupJoin, from the System.Linq namespace, we can group objects from one collection with any number of objects with from another collection. The method is challenging to use, as it receives three different Func implementations.

But: GroupJoin can be helpful when you need to create a grouped collection and do not want to write imperative code.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf