A day with .Net

My day to day experince in .net

Join two list using lambda expression

Posted by vivekcek on October 8, 2012

 List<Employee> employees = new List<Employee>()
            {
                new Employee(){ id="1", name= "Vivek"},
                 new Employee(){ id="2", name= "Chanthu"}
            };

            List<Address> address = new List<Address>()
            {
                new Address(){ Empid="1", Addres= "Eleyodu"},
                new Address(){ Empid="2", Addres= "Edakkadom"}
            };

            var query = employees.Join(
                address,
                emp => emp.id,
                adrs => adrs.Empid,
                (emp, adrs) => new { EmployeeName = emp, EmployeAddress = adrs })
                .Where(EmployeeAndAddress => EmployeeAndAddress.EmployeeName.id == EmployeeAndAddress.EmployeAddress.Empid).ToList();

            foreach (var item in query)
            {
                Console.WriteLine("Employee Name:{0},Address:{1}", item.EmployeeName.name, item.EmployeAddress.Addres);
            }
class Employee
        {
            public string id;
            public string name;
            public string department;
        }
        class Address
        {
            public string Empid;
            public string Addres;
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s