Cross Join in Linq

by Sachin Singh


Posted on Friday, 12 February 2021

Tags: Cross Join in Linq

In SQL Server a cross join produces Cartesian product of two tables which means each record of left table is joined with every record on the right table as shown in the diagram , below.

Cross Join in Sql Server
Cross Join in SQL Server

But with Linq we don't query a database tables directly , rather we query the Collection of Objects .The Entity Framework converts the tables into collection of objects , and we simply query those collections (DbSets) and then the Linq provider translates our query to corresponding T-SQL.

How to think
How to think when working with LINQ

The entity Framework will generate following domain model on the basis of Employee and Department table mentioned above.

Entity Model generated by EF
Entity Model generated by EF

In Linq we have no specific Join operator to perform a cross join , To implement Cross Join using extension method syntax, we could either use SelectMany() method or Join() method

Cross Join in Method Syntax

Implementing cross join using SelectMany()


       public static void Main()
        {
            OrganizationContext db = new OrganizationContext();
            var result = db.Employees
                        .SelectMany(e => db.Departments, (e, d) => new { e, d });
            foreach (var item in result)
            {
                Console.WriteLine("{0}    {1}", item.d.Name, item.e.Name);
                Console.WriteLine();
            }
            Console.ReadLine();
        }

Implementing cross join using Join()


      public static void Main()
        {
            OrganizationContext db = new OrganizationContext();
            var result = db.Employees
                                      .Join(db.Departments,
                                                e => true,
                                                d => true,
                                                (e, d) => new { e, d });       

            foreach (var item in result)
            {
                Console.WriteLine("{0}    {1}", item.d.Name, item.e.Name);
                Console.WriteLine();
            }
            Console.ReadLine();
        }

Output:

Outpu
Output

Cross Join in Query Syntax

A simple projection from two collections without joining them produces a cross join in linq


    public static void Main()
        {
            OrganizationContext db = new OrganizationContext();
            var result = from e in db.Employees
                          from d in db.Departments
                          select new { e, d };

            foreach (var v in result)
            {
                Console.WriteLine(v.e.Name + "\t" + v.d.Name);
            }
           
            Console.ReadLine();
        }

In this way we implement Cross Join in Linq, when we cross join two sequences, every element in the first collection is combined with every element in the second collection. The number of elements in the resultant sequence will always be the product of the number of items in each sequence.