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:


Share this

Previous
Next Post »

1 comments:

comments
April 9, 2021 at 11:49 PM delete

A full-stack web development course will teach you how to design and develop complete websites from start to finish. You will learn how to work on different web development aspects, including front-end, back-end, databases, debugging, and testing. Whether you’re interested in becoming a Front-End Developer or want to work in Back End Technologies, this course will give you all the skills you need to become a full-stack web developer and build a portfolio of projects.
full stack development course in hyderabad
full stack development Training in hyderabad

Reply
avatar