Showing posts sorted by relevance for query LINQ Tutorial. Sort by date Show all posts
Showing posts sorted by relevance for query LINQ Tutorial. Sort by date Show all posts

Join Operators in LINQ Tutorial

Joining refers to an operation in which data sources with difficult to follow relationships with each other in a direct way are targeted.
Show Examples
OperatorDescriptionC# Query Expression SyntaxVB Query Expression Syntax
JoinThe operator join two sequences on basis of matching keysjoin … in … on … equals …From x In …, y In … Where x.a = y.a
GroupJoinJoin two sequences and group the matching elementsjoin … in … on … equals … into …Group Join … In … On …

Example of Join - Query Expression

using System;
using System.Collections.Generic;
using System.Linq;

namespace Operators
{
    class JoinTables
    {
        class DepartmentClass
        {
            public int DepartmentId { get; set; }
            public string Name { get; set; }
        }

        class EmployeeClass
        {
            public int EmployeeId { get; set; }
            public string EmployeeName { get; set; }
            public int DepartmentId { get; set; }
        }

        static void Main(string[] args)
        {
            List<DepartmentClass> departments = new List<DepartmentClass>();
            departments.Add(new DepartmentClass { DepartmentId = 1, Name = "Account" });
            departments.Add(new DepartmentClass { DepartmentId = 2, Name = "Sales" });
            departments.Add(new DepartmentClass { DepartmentId = 3, Name = "Marketing" });
            departments.Add(new DepartmentClass { DepartmentId = 4, Name = "Packing" });
            departments.Add(new DepartmentClass { DepartmentId = 5, Name = "Store" });
            departments.Add(new DepartmentClass { DepartmentId = 6, Name = "HR" });


            List<EmployeeClass> employees = new List<EmployeeClass>();
            employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 1, EmployeeName = "William" });
            employees.Add(new EmployeeClass { DepartmentId = 2, EmployeeId = 2, EmployeeName = "Miley" });
            employees.Add(new EmployeeClass { DepartmentId = 2, EmployeeId = 3, EmployeeName = "Benjamin" });
            employees.Add(new EmployeeClass { DepartmentId = 3, EmployeeId = 4, EmployeeName = "Mack" });
            employees.Add(new EmployeeClass { DepartmentId = 3, EmployeeId = 5, EmployeeName = "Sam" });
            employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 6, EmployeeName = "Rock" });
            employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 5, EmployeeName = "Sam" });
            employees.Add(new EmployeeClass { DepartmentId = 1, EmployeeId = 6, EmployeeName = "Rock" });


            var list = (from e in employees
                        join d in departments on e.DepartmentId equals d.DepartmentId
                        select new
                        {
                            EmployeeName = e.EmployeeName,
                            DepartmentName = d.Name
                        });

            foreach (var e in list)
            {
                Console.WriteLine("Employee Name = {0} , Department Name = {1}",
                                  e.EmployeeName, e.DepartmentName);
            }

            Console.WriteLine("\nPress any key to continue.");
            Console.ReadKey();
        }
    }
}

When the above code of C# or VB is compiled and executed, it produces the following result: