Monday 2 January 2017

Join Two collection using Linq


In this post we are going to see how to join the two collection using Linq.



public abstract class Person
{
    public int ID { setget; }

    public string Name { setget; }

    public int PhoneNumber { setget; }

    public string Address { setget; }
}


public class Student:Person
{
    public string Department { setget; }

    public int TotalMarks { setget; }

}


public class Employee : Person
{
    public string Designation { set; get; }

    public string Department { set; get; }

    public int CollegeStudentId { set; get; }
}








        List<Student> StudentList = new List<Student>() {

            new Student(){ ID =4,
Name="Suresh",
Address="Porur",
TotalMarks=350,
Department="ECE",
PhoneNumber=12321312
},
            new Student(){ ID=2,
Name="Ramu",
Address="Delhi",
TotalMarks=450,
Department="CSE",
PhoneNumber=23233112
},
             new Student(){
ID=1,
Name="Rajesh",
Address="Madurai",
TotalMarks=400,
Department="ECE",
PhoneNumber=3434322
},
            new Student(){
ID=3,
Name="Rajesh",
Address="Chennai",
TotalMarks=460,
Department="CSE",
PhoneNumber=43434223
},
            new Student(){
ID=5,
Name="Hanish",
Address="Salem",
TotalMarks=420,
Department="EEE",
PhoneNumber=34423232
}
        };




        List<Employee> employeeList = new List<Employee>() {
            new Employee(){
ID = 103,
Name="Rajesh",
Department="Development",
Address="TN",
PhoneNumber=232323,
CollegeStudentId=1,
Designation="Architect"
},
            new Employee(){ 
ID = 102,
Name="Suresh",
Department="Development",
Address="Chennai",
PhoneNumber=4444333,
CollegeStudentId=4,
Designation="Architect"
},
            new Employee(){ 
ID = 104,
Name="Rajesh",
Department="Development",
Address="Pune",
PhoneNumber=523252,
CollegeStudentId=3,
Designation="Manager"
},
            new Employee(){
ID = 105,
Name="Ramu",
Department="Development",
Address="Delhi",
PhoneNumber=6756342,
CollegeStudentId=2,
Designation="SSE"
},
            new Employee(){ 
ID = 101,
Name="Hanish",
Department="Development",
Address="US",
PhoneNumber=238564334,
CollegeStudentId=5,
Designation="TL"
}
        };






  var joindata = employeeList.Join(StudentList,
                                e => e.CollegeStudentId, /* Employee type join  condition */
                                s => s.ID,              /* Student type join condition */
                                (e, s) => new {   /* Result function with both type input */
                                    EmpName = e.Name,
                                    EmpId = e.ID,
                                    Marks = s.TotalMarks,
                                    Department = s.Department
                                });


        foreach (var item in joindata)
        {
            Console.WriteLine(string.Format("ID : {0}, Name : {1}, Marks : {2}"
                      item.EmpId, item.EmpName, item.Marks));
        }



output:
***********

ID : 103, Name : Rajesh, Marks : 400
ID : 102, Name : Suresh, Marks : 350
ID : 104, Name : Rajesh, Marks : 460
ID : 105, Name : Ramu, Marks : 450
ID : 101, Name : Hanish, Marks : 420




LinqToSql
************

        var sqljoindata =   from e in employeeList
                            join s in StudentList
                            on e.CollegeStudentId equals s.ID
                            select new
                            {
                                EmpName = e.Name,
                                EmpId = e.ID,
                                Marks = s.TotalMarks,
                                Department = s.Department
                            };



        foreach (var item in sqljoindata)
        {
            Console.WriteLine(string.Format("ID : {0}, Name : {1}, Marks : {2}",
                 item.EmpId, item.EmpName, item.Marks));
        }

        Console.Read();


output:
***********

ID : 103, Name : Rajesh, Marks : 400
ID : 102, Name : Suresh, Marks : 350
ID : 104, Name : Rajesh, Marks : 460
ID : 105, Name : Ramu, Marks : 450
ID : 101, Name : Hanish, Marks : 420



From this post we can learn how to join the two collections using Linq in various ways

1 comment: