Joining common values from two sequences using the LINQ Intersect operator
September 7, 2017 1 Comment
Say you have the following two sequences:
string[] first = new string[] {"hello", "hi", "good evening", "good day", "good morning", "goodbye" };
string[] second = new string[] {"whatsup", "how are you", "hello", "bye", "hi"};
If you’d like to find the common elements in the two arrays and put them to another sequence then it’s very easy with the Intersect operator:
IEnumerable<string> intersect = first.Intersect(second);
foreach (string value in intersect)
{
Console.WriteLine(value);
}
The ‘intersect’ variable will include “hello” and “hi” as they are common elements to both arrays.