TheDeveloperBlog.com

Home | Contact Us

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

C# Join Example: LINQ

These C# examples use the join keyword in query expressions. They require System.Linq.

Join is a keyword in LINQ. As with other query languages (such as SQL) joining matches every element in two collections based on some condition.

We use this keyword in a query expression (beginning with from).

Example. This first example shows the Join extension method from the System.Linq namespace. And then it shows the equivalent join query expression syntax. Internally the join query is translated to the Join() extension method by the C# compiler.

Extension Method

Tip: You can read in painful detail how query expressions are translated in the chapter on Expressions (7) in the C# specification.

But: This material is complex, tiresome and not all that useful for actual development.

With Join, we provide delegates (lambda expressions) as arguments. With the query expression, we specify this in a clause. Both examples join ints1 with ints2. They select every element in ints1 that exists in ints2 with an added 1.

So: If ints1 contains an element of value 4, it is selected only if there is an element of value of 5 in ints2.

Int Array

Based on:

.NET 4.5

C# program that uses Join

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	// Array 1.
	var ints1 = new int[3];
	ints1[0] = 4;
	ints1[1] = 3;
	ints1[2] = 0;

	// Array 2.
	var ints2 = new int[3];
	ints2[0] = 5;
	ints2[1] = 4;
	ints2[2] = 2;

	{
	    // Join with method call.
	    var result = ints1.Join<int, int, int, int>(ints2,
		x => x + 1,
		y => y,
		(x, y) => x);

	    // Display results.
	    foreach (var r in result)
	    {
		Console.WriteLine(r);
	    }
	}

	{
	    // Join with query expression.
	    var result = from t in ints1
			 join x in ints2 on (t + 1) equals x
			 select t;

	    // Display results.
	    foreach (var r in result)
	    {
		Console.WriteLine(r);
	    }
	}
    }
}

Output

4  (First loop)
3
4  (Second loop)
3

In my view, the second syntax form—with the query expression—is easier to read. This would make it more maintainable and less likely to create bugs. Code that is easier to read is often better.

Tip: The Join and GroupJoin methods are hard to use directly. They are best used indirectly, through the query syntax.

Example 2. This example program uses two classes for storing data. It uses the Customer class, which has an ID and a Name for each instance, and the Order class, which has an ID and a Product string for each instance.

Main: In the Main method, we use the abbreviated C# syntax for constructing the array instances. Each has four elements.

Finally: The query expression uses the join keyword to create new objects where the ID equals.

C# program that uses join and equals in query

using System;
using System.Linq;

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

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

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

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

	// Join on the ID properties.
	var query = from c in customers
		    join o in orders on c.ID equals o.ID
		    select new { c.Name, o.Product };

	// Display joined groups.
	foreach (var group in query)
	{
	    Console.WriteLine("{0} bought {1}", group.Name, group.Product);
	}
    }
}

Output

Sam bought Book
Dave bought Game
Julia bought Computer
Sue bought Shirt

In the query expression, the customers array is used and each element from that array is identified as "c". The orders array is also used, and each element from that is identified as "o".

And: The join takes the ID property from "c" and "o" and matches them. As the matches occur, new elements are created in the result.

SQL: This syntax is basically the same as in SQL. Occasionally it can be easier to develop logic in this style of language.

Discussion. Join is different from GroupJoin. With Join, we cannot create a new type to store multiple results together in a single element. We can only select a single value. But GroupJoin is invoked with the join query expression keyword.

GroupJoin

Part of this article is based on the C# specification. In the specification, detailed examples are given for how query expressions are processed. This is near the end of the horrifying 143-page chapter on expressions.

Note: Please remember that thoroughness is a valuable attribute for language specifications.

Summary. Using the LINQ extensions in the C# language, we can implement join expressions. This can make some matching routines more concise in a program. It can help us convert SQL logic to C# code in some cases as well.

Also: The Join extension method can be directly used. But this complicates syntax due to its use of three delegate methods.

Delegates


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