4

I have two IList<string> a and b. I want to find out what strings are in both a and b using LINQ.

kurasa
  • 4,977
  • 8
  • 36
  • 53

1 Answers1

9

Use Intersect:

Produces the set intersection of two sequences.

a.Intersect(b)

Example usage:

IList<string> a = new List<string> { "foo", "bar", "baz" };
IList<string> b = new List<string> { "baz", "bar", "qux" };

var stringsInBoth = a.Intersect(b);

foreach (string s in stringsInBoth)
{
    Console.WriteLine(s);
}

Output:

bar
baz
Community
  • 1
  • 1
Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434