C# Program to Join Multiple Data Sources using LINQ
Last Updated :
06 Dec, 2021
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It provides the ability to .NET languages to create queries to retrieve data from the data source. In this article, we will discuss how to join multiple data sources using LINQ. Here data source means list. So we are using list collection to create three data sources with student details, then join the data based on id, which is common in all the lists using the join keyword.
Syntax:
from iterator1 in data1
join iterator2 in data2
on iterator1.column_name equals iterator2.column_name
join iterator3 in data3
on iterator1.column_name equals iterator3.column_name
--------------------------------------------------
--------------------------------------------------
join iteratorn in datan
on iterator1.column_name equals iteratorn.column_name
Where data is the list of the data source and iterator is used to get the data from the particular data source
Return: It will return the matching rows based on the column_names compared.
Example:
Input:
Student
new Student{id = 7058, name = "sravan kumar", dept_id = 1, add_id = 21},
new Student{id = 7059, name = "jyothika", dept_id = 2, add_id = 22},
new Student{id = 7072, name = "harsha", dept_id = 1, add_id = 22},
new Student{id = 7076, name = "khyathi", dept_id = 4, add_id = 27},
Department
new Department{dept_id = 1, dept_name = "CSE"},
new Department{dept_id = 2, dept_name = "CSE"},
new Department{dept_id = 3, dept_name = "IT"},
Address
new Address{add_id = 21, address_name = "hyd"},
new Address{add_id = 22, address_name = "railu-peta"},
new Address{add_id = 24, address_name = "chenchu-peta"},
Output:
ID: 7058--> Name: sravan kumar--> Department: CSE--> Address: hyd
ID: 7059--> Name: jyothika--> Department: CSE--> Address: railu-peta
ID: 7072--> Name: harsha--> Department: CSE--> Address: railu-peta
Approach
1. Create three data sources by using a list named Student, Department, and Address by declaring the variables.
2. Add values to these lists.
3. Perform the join based on student id, department id, and address id.
var result = (from stu in students
join dept in departments on stu.dept_id equals dept.dept_id
join add in addresses on stu.add_id equals add.add_id).ToList();
4. Select the data using select() method.
select new
{
ID = stu.id,
Name = stu.name,
DeptName = dept.dept_name,
address = add.address_name
}
5. Display using for each loop.
foreach (var e in result)
{
Console.WriteLine("\tID: " + e.ID + "--> Name: " +
e.Name + "--> Department: " +
e.DeptName + "--> Address: " + e.address);
}
Example:
C#
// C# program to join multiple data sources
// Using LINQ
using System;
using System.Linq;
using System.Collections.Generic;
// Variables for Student list
public class Student
{
public int id;
public string name;
public int dept_id;
public int add_id;
}
// Variables for Department list
public class Department
{
public int dept_id;
public string dept_name;
}
// Variables for Address list
public class Address
{
public int add_id;
public string address_name;
}
class GFG{
// Driver code
static void Main(string[] args)
{
// Enter data for Student list
List<Student> students = new List<Student>()
{
new Student{ id = 7058, name = "sravan kumar",
dept_id = 1, add_id = 21 },
new Student{ id = 7059, name = "jyothika",
dept_id = 2, add_id = 22 },
new Student{ id = 7072, name = "harsha",
dept_id = 1, add_id = 22 },
new Student{ id = 7076, name = "khyathi",
dept_id = 4, add_id = 27 },
};
List<Department> departments = new List<Department>()
{
new Department{ dept_id = 1, dept_name = "CSE" },
new Department{ dept_id = 2, dept_name = "CSE" },
new Department{ dept_id = 3, dept_name = "IT " },
};
List<Address> addresses = new List<Address>()
{
new Address{ add_id = 21, address_name = "hyd" },
new Address{ add_id = 22, address_name = "railu-peta" },
new Address{ add_id = 24, address_name = "chenchu-peta" },
};
// Join the students and other two tables
var result = (from stu in students
join dept in departments on stu
.dept_id equals dept
.dept_id
join add in addresses on stu
.add_id equals add.add_id
select new
{
ID = stu.id, Name = stu.name,
DeptName = dept.dept_name,
address = add.address_name
}).ToList();
// Display the result
foreach(var e in result)
{
Console.WriteLine("\tID: " + e.ID + "--> Name: " +
e.Name + "--> Department: " +
e.DeptName + "--> Address: " + e.address);
}
}
}
Output:
ID: 7058--> Name: sravan kumar--> Department: CSE--> Address: hyd
ID: 7059--> Name: jyothika--> Department: CSE--> Address: railu-peta
ID: 7072--> Name: harsha--> Department: CSE--> Address: railu-peta
Similar Reads
C# Program to Sort Student Names in Descending Order using LINQ
Given a list of student names, now our task is to sort the names given in the list in descending order using the LINQ, This task can be done using OrderByDescending() method of LINQ. This method is used to sort the elements in the collection in descending order. While using this method in the LINQ q
2 min read
C# Program to Find the Index of Even Numbers using LINQ
Given an array, now our task is to find the index value of the even numbers present in the given array using LINQ. LINQ is known as Language Integrated Query and was introduced in .NET 3.5. It gives the power to .NET languages to generate queries to retrieve data from the data source. So to do this
2 min read
C# Program to Join Employee and Department Class using LINQ Join Query
Given two classes named as Employee and Department, now we join Employee and Department class with the help of LINQ join Query. So to this task, we use the Join clause. This clause is used to join two data sources into one source which has some common attributes. It always takes two data sources and
3 min read
C# Program to Show the Usage of LINQ Aggregate() Method
In LINQ, the aggregation function is the function that serves the purpose of calculating one value from a collection of values. Or we can say that the Aggregate() method is used to perform aggregation operations on the values of a collection. In simple words, the Aggregate() method implements a numb
2 min read
C# Program to Sort a List of Employees Based on Salary using LINQ
Given a list of employees, now we sort the list of employees according to their salary using LINQ. So to this task, we use the OrderBy() method. This method is used to sort the elements of the specified sequence in ascending order. Example: Input: {id = 101, Name = Rohit, Salary = 50000, Department
3 min read
C# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
Given a list of objects, we need to find the integer double numbers from the list of objects and sort them using LINQ. So this task can be done using the OfType() method and then the sorting of numbers can be done by using OrderBy() method. Let discuss them one by one along with their syntax: 1. OfT
2 min read
C# Program to Check a Specified City Exists in the List Collection using LINQ
Given a list, we need to check if a specified city exists in the given list using LINQ. So we can do this task using contains() method. This method is used to check whether a sequence contains a specified element or not. The method overloads in the following ways: Contains<TSource>(IEnumerable
3 min read
C# Program to Check the Length of Courses is More than 2 Characters Using LINQ
Given an array that contains a list of different courses like "DSA", "OS", "JavaScript", etc., now our task is to check the length of all the courses present in the given array is more than 2 characters or not with the help of LINQ. So to do our task we use the All() method of LINQ. This method retu
2 min read
C# Program to Demonstrate the Example of LINQ Union() Method with StringComparer
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives a feature to .NET languages to create queries to retrieve data from the data source. Here in this article, we will demonstrate the example of the LINQ Union() method with the StringComparer. 1. Union() Method: This
2 min read
C# Program to Calculate the Sum of Array Elements using the LINQ Aggregate() Method
Given an array of integers, now we calculate the sum of array elements. So we use the Aggregate() method of LINQ. This method applies a function to all the elements of the source sequence and calculates a cumulative result and return value. This method is overloaded in three different ways: Aggregat
2 min read