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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is a Neural Network?
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamenta
14 min read