Skip to content
Related Articles

Related Articles

C# Program to Join Multiple Data Sources using LINQ

Improve Article
Save Article
Like Article
  • 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!