Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Cross Join in LINQ
Language Integrated Query (LINQ) is a powerful tool in C# for data manipulation, allowing for efficient and expressive data access and manipulation. One of the operations you can perform with LINQ is the cross join operation, which creates a Cartesian product between two data sources.
Understanding Cross Join
Cross join, also known as Cartesian product, is a type of join operation that matches each row of the first table with every row of the second table. If the first table has n rows and the second table has m rows, the result is a table with n×m rows.
Syntax
Following is the syntax for implementing a cross join using LINQ query syntax
var crossJoin = from item1 in collection1
from item2 in collection2
select new { Property1 = item1, Property2 = item2 };
Following is the syntax using LINQ method syntax
var crossJoin = collection1.SelectMany(item1 => collection2,
(item1, item2) => new { Property1 = item1, Property2 = item2 });
Using Cross Join with Query Syntax
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> colors = new List<string> { "Red", "Green", "Blue" };
List<string> items = new List<string> { "Pen", "Book", "Glass" };
var crossJoin = from color in colors
from item in items
select new { Color = color, Item = item };
foreach (var element in crossJoin) {
Console.WriteLine($"{element.Color}, {element.Item}");
}
}
}
The output of the above code is
Red, Pen Red, Book Red, Glass Green, Pen Green, Book Green, Glass Blue, Pen Blue, Book Blue, Glass
Using Cross Join with Method Syntax
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2 };
List<char> letters = new List<char> { 'A', 'B' };
var crossJoin = numbers.SelectMany(num => letters,
(num, letter) => new { Number = num, Letter = letter });
foreach (var pair in crossJoin) {
Console.WriteLine($"Number: {pair.Number}, Letter: {pair.Letter}");
}
}
}
The output of the above code is
Number: 1, Letter: A Number: 1, Letter: B Number: 2, Letter: A Number: 2, Letter: B
Using Cross Join with Complex Objects
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Student {
public string Name { get; set; }
public int Id { get; set; }
}
class Course {
public string CourseName { get; set; }
public string Code { get; set; }
}
class Program {
static void Main() {
List<Student> students = new List<Student> {
new Student { Name = "Alice", Id = 1 },
new Student { Name = "Bob", Id = 2 }
};
List<Course> courses = new List<Course> {
new Course { CourseName = "Math", Code = "M101" },
new Course { CourseName = "English", Code = "E101" }
};
var enrollments = from student in students
from course in courses
select new { StudentName = student.Name, CourseName = course.CourseName };
foreach (var enrollment in enrollments) {
Console.WriteLine($"{enrollment.StudentName} can enroll in {enrollment.CourseName}");
}
}
}
The output of the above code is
Alice can enroll in Math Alice can enroll in English Bob can enroll in Math Bob can enroll in English
Performance Considerations
| Collection 1 Size | Collection 2 Size | Result Size | Performance Impact |
|---|---|---|---|
| 10 | 10 | 100 | Acceptable |
| 100 | 100 | 10,000 | Moderate |
| 1,000 | 1,000 | 1,000,000 | High - Use with caution |
Conclusion
Cross join in LINQ creates a Cartesian product between two collections, generating all possible combinations. While powerful for data analysis and combination generation, be mindful of performance implications as the result size grows exponentially with input collection sizes.
