.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Merge two lists using LINQ's Union in C#

Posted By:volvo       Posted Date: December 11, 2013    Points: 40    Category: LINQ    URL: http://www.dotnetspark.com  

Code block explains that how to merge two lists using LINQ's Union in C#
 

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

namespace LinQMerge

{

internal class Program

{

//How to Merge two lists using LINQ's Union in C# ?

private static void Main(string[] args)

{

List<Employee> employees = new List<Employee>()

{

new Employee { EmployeeID = 1 , Name ="Jack"},

new Employee { EmployeeID = 2 , Name ="Peter"},

new Employee { EmployeeID = 3 , Name ="Thomas"},

new Employee { EmployeeID = 4 , Name ="William"}

};

List<Employee> employees2 = new List<Employee>()

{

new Employee { EmployeeID = 1 , Name ="Chris"},

new Employee { EmployeeID = 2 , Name ="Charles"},

new Employee { EmployeeID = 3 , Name ="Levi"},

new Employee { EmployeeID = 4 , Name ="Murach"}

};

//Merge the employees and Employees2 list

var MergedEmployees = employees.Union(employees2).ToList();

foreach (var item in MergedEmployees)

Console.WriteLine(item.Name);

Console.ReadLine();

}

}

public class Employee

{

public string Name { get; set; }

public int EmployeeID { get; set; }

}

}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend