I have two IList<string>
a and b. I want to find out what strings are in both a and b using LINQ.
Asked
Active
Viewed 891 times
4

kurasa
- 5,205
- 9
- 38
- 53
-
2Your question and title appear to contradict each other. Do you want the items that are in both, or do you want the differences? – Mark Byers Jun 07 '10 at 00:04
-
sorry only the one that are in both :) – kurasa Jun 07 '10 at 00:28
-
I know I am noob to linq but did the question have to voted down?? – kurasa Jun 07 '10 at 00:30
-
Don't worry about it - if you edit your question so that the title and the actual question agree with one another, people should remove their downvotes :) – Mark Carpenter Jun 07 '10 at 00:36
1 Answers
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
- 811,555
- 193
- 1,581
- 1,452