Wednesday 11 July 2018

Join Query in Linq C#





using ConsoleApplication2.Model;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            List<department> deplist = new List<department>

            {

                new department{deptid=1, deptname="HR"},

                new department{deptid=2, deptname="Marketing"},

                new department{deptid=3, deptname="IT"},

                new department{deptid=4, deptname="Accounts"},



            };



            List<Employee> emplist = new List<Employee>

            {

                new Employee{empid=101,name="ali",deptfkid=1},

                new Employee{empid=102,name="ahmed",deptfkid=2},

               new Employee{empid=103,name="ahsan",deptfkid=1},

               new Employee{empid=104,name="bilal",deptfkid=3},

               new Employee{empid=105,name="zahid",deptfkid=1},

                new Employee{empid=106,name="fahad",deptfkid=2},







            };





            var joinlist = emplist.Join(deplist, emp => emp.deptfkid, dep => dep.deptid, (emp, dep) => new

            {

                empid=emp.empid,

                name=emp.name,

                deptname=dep.deptname

           

            });



           



            foreach (var item in joinlist)

            {

                Console.WriteLine("-------------------");



                Console.WriteLine("id: "+item.empid);

                Console.WriteLine("Name: " + item.name);

                Console.WriteLine("Department: " + item.deptname);



                Console.WriteLine("-------------------");



            }



            Console.ReadKey();





        }

    }

}

----------------------------------------------------------------------------------------------------
 class department
    {
        public int deptid { get; set; }
        public string deptname { get; set; }

    }
-----------------------------------------------------------------------------------------------
 class Employee
    {

        public int  empid { get; set; }
        public string name { get; set; }
        public int deptfkid { get; set; }
    }

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...