Time for action – finding common names from two names' lists

Follow the given steps:

  1. Copy the following code snippet as a query to LINQPad:
    List<string> nameList1 = new List<string>(){"Anders", "David", "James", "Jeff", "Joe", "Erik"};
    List<string> nameList2 = new List<string>() {"Erik","David","Derik"};
    var commonNames = nameList1.Intersect(nameList2);
    foreach (var name in commonNames)
    Console.WriteLine(name);
  2. Run the query. You will see the following output:
    Time for action – finding common names from two names' lists

What just happened?

Intersect() returns an IEnumerable<T>, including only the elements that appear in both the collections. Intersect() has two overloaded versions. One works with the primitive ...

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.