Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Tuesday, 31 October 2017

INNER JOIN Example Using LINQ In C# Asp.Net

 asp.net, Asp.Net C#, csharp, guruji point, Gurujipoint, Inner Join using LINQ, LINQ, LINQ Examples, LINQ Practice     1 comment   


Introduction
In this article we will learn how to use join using LINQ in C#. Join example with LINQ using LIST collection. 


Previous Updates
In previous articles we have learnt  Maintain state of dynamic added userConrol in asp.net using c#. Insert Only Numeric values in Asp Textbox using Regex. 

Description:
Here i am using the List collection to store the temporary/static data. In your case may be it will the database data. 
Below is My two class just like two tables Student and Department.
        class Emp
        {
            public int EmpID { get; set; }
            public string EmpName { get; set; }
        }
        class Dep
        {
            public int DepID { get; set; }
            public string Department { get; set; }
            public int EmpID { get; set; }
        }
Below i fill the dummy data into List , it just like your SQL table data and now i want to retrieve only those employee records who are common with Departments.
Arrange Distinct Elements Of A List In Ascending Order Using LINQ
static void Main(string[] args)
{
    List<Emp> liEmp = new List<Emp>();
    liEmp.Add(new Emp { EmpID = 1, EmpName = "JP" });
    liEmp.Add(new Emp { EmpID = 2, EmpName = "AB" });
    liEmp.Add(new Emp { EmpID = 3, EmpName = "CD" });
    liEmp.Add(new Emp { EmpID = 6, EmpName = "EF" });
    liEmp.Add(new Emp { EmpID = 8, EmpName = "EF" });

    List<Dep> lstDep = new List<Dep>();
    lstDep.Add(new Dep { DepID = 1, Department = ".Net", EmpID = 1 });
    lstDep.Add(new Dep { DepID = 2, Department = "PHP", EmpID = 2 });
    lstDep.Add(new Dep { DepID = 3, Department = "ASP", EmpID = 3 });
    lstDep.Add(new Dep { DepID = 4, Department = "Android", EmpID = 4 });
    lstDep.Add(new Dep { DepID = 5, Department = "GD", EmpID = 5 });
    lstDep.Add(new Dep { DepID = 6, Department = "UI", EmpID = 6 });

    //InnerJoin Using LINQ 
    var finalData = from emp in liEmp
                    join dep in lstDep
         on emp.EmpID equals dep.EmpID
                    select new
                    {
                        emp.EmpID,
                        emp.EmpName,
                        dep.Department
                    };
    foreach (var data in finalData)
    {
        Console.WriteLine("EmpId = " + data.EmpID
                         + ",Emp Name = " + data.EmpName +
                        ",Department = " + data.Department);
    }
    Console.ReadLine();
}
Output:
EmpId = 1,Emp Name = JP,Department = .Net
EmpId = 2,Emp Name = AB,Department = PHP
EmpId = 3,Emp Name = CD,Department = ASP
EmpId = 6,Emp Name = EF,Department = UI

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home
View mobile version

1 comment:

  1. fuat18 August 2023 at 16:34

    kuşadası
    milas
    çeşme
    bağcılar
    muğla

    AZQ6

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write