From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

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

The Union method

The Union method

- [Instructor] Continuing on our journey of looking at the set operators and this video we'll look at the UNION operator, It's purpose is to merge two sequences together and remove all the duplicates. So the syntax is similar to what we've seen. You have the first sequence, you'd call that union on that, and you pass the second sequence as an argument. In this example, I have two list of integers, eight items in the first, six items in the second, and I was going to remove all duplicates. Even if those duplicates happen to be in the same sequence. So example here, I've got eight twice in numbers A and a single time in numbers B, but I'll end up with a single eight in the entire output sequence like this. Let's start with 14 items. The resulting union is 10 items. Notice that the order is the order that they're shown in the lists. So it starts off with zero, two, four, these are the items in the first list. And then…

Contents