Cross Join in LINQ - GeeksforGeeks

01 Jan, 2020
In LINQ, the cross join is a process in which the elements of two sequences are combined with each other means the element of first sequence or collection is combined with the elements of another sequence or collection without any key selection or any filtering condition and the number of elements present in the resulting sequence is equal to the product of the elements in the two source sequences or collections. Or in other words, we can say that cross join will produce a Cartesian product. Cross join is also known as full join. In cross join, we do not require on keyword which specifies the condition for joining.
Let us discuss this concept with the help of given examples:
Example 1:
// C# program to illustrate the concept// of the cross joinusing System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee {      public int emp_id    {        get;        set;    }      public string emp_name    {        get;        set;    }    public string emp_lang    {        get;        set;    }    public int dpt_id    {        get;        set;    }}  // Employee department detailspublic class Department {      public int dpt_id    {        get;        set;    }      public string emp_dept    {        get;        set;    }}  class GFG {      // Main method    static public void Main()    {        List<Employee> emp = new List<Employee>() {              new Employee() { emp_id = 101, emp_name = "Amit",                             emp_lang = "Java", dpt_id = 1 },                  new Employee() { emp_id = 102, emp_name = "Mohit",                                    emp_lang = "C#", dpt_id = 2 },                  new Employee() { emp_id = 103, emp_name = "Sona",                                 emp_lang = "Java", dpt_id = 1 },                  new Employee() { emp_id = 104, emp_name = "Lana",                                  emp_lang = "C++", dpt_id = 3 },                  new Employee() { emp_id = 105, emp_name = "Roma",                                   emp_lang = "C#", dpt_id = 2 },          };          List<Department> Dpt = new List<Department>() {              new Department() { dpt_id = 1, emp_dept = "Designing" },            new Department() { dpt_id = 2, emp_dept = "Development" },            new Department() { dpt_id = 3, emp_dept = "JE" },          };          // Implementing Cross join using query syntax        var res = from first in emp            from second in Dpt                select new {                    Employee_name = first.emp_name,                    Department_name = second.emp_dept                  };          // Display result        foreach(var val in res)        {              Console.WriteLine(" Employee Name: {0} || Department Name: {1}",                                    val.Employee_name, val.Department_name);        }    }}
Output:
 Employee Name: Amit || Department Name: Designing
 Employee Name: Amit || Department Name: Development
 Employee Name: Amit || Department Name: JE
 Employee Name: Mohit || Department Name: Designing
 Employee Name: Mohit || Department Name: Development
 Employee Name: Mohit || Department Name: JE
 Employee Name: Sona || Department Name: Designing
 Employee Name: Sona || Department Name: Development
 Employee Name: Sona || Department Name: JE
 Employee Name: Lana || Department Name: Designing
 Employee Name: Lana || Department Name: Development
 Employee Name: Lana || Department Name: JE
 Employee Name: Roma || Department Name: Designing
 Employee Name: Roma || Department Name: Development
 Employee Name: Roma || Department Name: JE

Example 2:
// C# program to illustrate the concept// of cross joinusing System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee {      public int emp_id    {        get;        set;    }      public string emp_name    {        get;        set;    }    public string emp_lang    {        get;        set;    }    public int dpt_id    {        get;        set;    }}  // Employee department detailspublic class Department {      public int dpt_id    {        get;        set;    }      public string emp_dept    {        get;        set;    }}  class GFG {      // Main method    static public void Main()    {        List<Employee> emp = new List<Employee>() {              new Employee() { emp_id = 101, emp_name = "Amit",                              emp_lang = "Java", dpt_id = 1 },              new Employee() { emp_id = 102, emp_name = "Mohit",                                emp_lang = "C#", dpt_id = 2 },          };          List<Department> Dpt = new List<Department>() {              new Department() { dpt_id = 1, emp_dept = "Designing" },            new Department() { dpt_id = 2, emp_dept = "Development" },          };          // Implementing Cross join using         // SelectMany method and lambda        // expression        var res_1 = Dpt.SelectMany(e => emp,(x, y) => new {                                Department_name = x.emp_dept,                                  Employee_name = y.emp_name});          // Display result        foreach(var val in res_1)        {              Console.WriteLine(" Department Name: {0} || Employee Name: {1}",                                    val.Department_name, val.Employee_name);        }          // Implementing Cross join using         // Join method and lambda expression        var res_2 = Dpt.Join(emp, x => true, y => true, (x, y) => new {                                          Department_name = x.emp_dept,                                          Employee_name = y.emp_name});          Console.WriteLine();                  // Display result        foreach(var val in res_2)        {              Console.WriteLine(" Department Name: {0} || Employee Name: {1}",                                    val.Department_name, val.Employee_name);        }    }}
Output:
 Department Name: Designing || Employee Name: Amit
 Department Name: Designing || Employee Name: Mohit
 Department Name: Development || Employee Name: Amit
 Department Name: Development || Employee Name: Mohit

 Department Name: Designing || Employee Name: Amit
 Department Name: Designing || Employee Name: Mohit
 Department Name: Development || Employee Name: Amit
 Department Name: Development || Employee Name: Mohit

CSharp LINQ
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
Convert String to Character Array in C#
C# | How to insert an element in an Array?
Lambda Expressions in C#