LINQ Intersect Operator

For example, we have two collections A = { 1, 2, 3 } and B = { 3, 4, 5 }. Intersect operator will find common elements in both sequences. { 3 } element is common in both sequences. So Intersect operator only shows one element in the resulting sequence { 3 }.

In the above example, we have two department of employees A and B. We need to find those employees who are working in both departments. We call Intersect operator method on employeesInDeptA and pass employeesInDeptB variable as a parameter. In the result we got employees names "Kapil" and "Sumit" which are common in both sequences.

By default, Intersect operator can not compare custom types. To remove this shortcomings, we have to use second overload method of Intersect operator. In this method, we can use IEqualityComparer interface which can be used for comparing custom types. Below is the example: