join Examples (LINQ)
This page was last reviewed on May 6, 2023.
Dot Net Perls
Join. This 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). We create new data structures (projections) based on the contents of elements.
First example. We use the Join extension method, and a join query expression. The Join and GroupJoin methods are best used indirectly, through the query syntax.
Part 1 We create 2 arrays. The goal of our joins will be to get elements from the first array based on the values in the second array.
Part 2 We call the Join method with lambdas. The lambdas join if the first value from ints1 is present in ints2 with 1 added.
Part 3 We use a query expression version. This performs the same joins as the extension method call.
Result 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
using System; using System.Linq; // Part 1: create array 1 and array 2. var ints1 = new int[3]; ints1[0] = 4; ints1[1] = 3; ints1[2] = 0; var ints2 = new int[3]; ints2[0] = 5; ints2[1] = 4; ints2[2] = 2; { // Part 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); } } { // Part 3: 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); } }
4 (First loop) 3 4 (Second loop) 3
Example 2. This example program uses two classes for storing data. Join query syntax is basically the same as in SQL. Occasionally it is easier to develop logic in this style.
Step 1 We create the classes. Customer has an ID and a Name for each instance. And Order has an ID and a Product string for each instance.
Step 2 The join takes the ID property from "c" and "o" and matches them. As the matches occur, new elements are created in the result.
Step 3 We evaluate the query expression by using it in a foreach-loop. We print out the customers and their orders.
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() { // Step 1: example customers and orders. 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"} }; 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"} }; // Step 2: 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 }; // Step 3: display joined groups. foreach (var group in query) { Console.WriteLine("{0} bought {1}", group.Name, group.Product); } } }
Sam bought Book Dave bought Game Julia bought Computer Sue bought Shirt
Fluent syntax, join IDs. This example joins two arrays of elements on the ID property of each element. It uses Animal and Medication instances.
So A cat with a specific ID may have 2 medications prescribed to it. The Join call resolves all of these.
And We use the Join() method with lambda expression arguments. This is equivalent a query expression with the "join" clause.
using System; using System.Linq; class Animal { public int ID { get; set; } public string Breed { get; set; } } class Medication { public int ID { get; set; } public string Type { get; set; } } class Program { static void Main() { // Example animals in veterinarian office. var animals = new Animal[] { new Animal{ID = 0, Breed = "cat"}, new Animal{ID = 10, Breed = "dog"} }; // Example medications (prescriptions). var medications = new Medication[] { new Medication{ID = 10, Type = "antibiotic"}, new Medication{ID = 0, Type = "sedative"}, new Medication{ID = 0, Type = "antihistamine"} }; // Use fluent join syntax on ID. var query = animals.Join(medications, a => a.ID, m => m.ID, (a, m) => new { a.Breed, m.Type }); // Results. foreach (var group in query) { Console.WriteLine($"{group.Breed} prescribed {group.Type}"); } } }
cat prescribed sedative cat prescribed antihistamine dog prescribed antibiotic
GroupJoin. Join is different from GroupJoin. With Join, we cannot create a new type to store multiple results together in a single element.
And We can only select a single value. But GroupJoin is invoked with the join query expression keyword.
String.Join. The "join" keyword in LINQ is not related to the string.Join method, which combines an array or List of strings into one string. Thread.Join is also a different thing.
Thread Join
Notes, internals. The join query is translated to the Join() extension method. You can read how query expressions are translated in the chapter on Expressions (7) in the C# specification.
Info This material is complex, tiresome and not all that useful for actual development.
Using the LINQ extensions in the C# language, we can implement join expressions. This can make some matching routines more concise in a program.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 6, 2023 (rewrite).
© 2007-2024 Sam Allen.