Time for action – finding all names from the list, removing duplicates

Follow the given steps:

  1. Copy the following code snippet as a query to LINQPad. Using the previous example, the change is highlighted:
    List<string> nameList1 = new List<string>(){"Anders", "David", "James", "Jeff", "Joe", "Erik"};
    List<string> nameList2 = new List<string>(){"Erik","David","Derik"};
    var commonNames = nameList1.Union(nameList2);
    foreach (var name in commonNames)
    Console.WriteLine(name);
  2. Run the query. You should see the following output:
    Time for action – finding all names from the list, removing duplicates

What just happened?

Union() returns a list of all the strings in both the collections, removing duplicates. Like Itersect(), it ...

Get .NET 4.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.