From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

The Intersect method

The Intersect method

- We can use the intersect method to find items that occur in both sets, or sequences of data. So in my example, I have two lists of integers. The first one consists of all even numbers, except for the number nine. The second set consists of all odd numbers, except for the number eight. And you'll notice that eight is also duplicated twice in the first list. When I call intersect using this syntax, take the first sequence, call intersect, and pass in the second sequence. Link will figure out the items that are in both sets, which in my case is five. Yes, that's five's in both sets. We know eight's in both sets. It's in there three times. And nine's in both sets. Simple to use. Quick and easy way to get items that occur in both sets of information.

Contents