This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn about set operations in LINQ using Distinct, Union, Intersect, Except and Concat.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We're still using our list of birds here.
0:00
So if you need to, pause the video and
0:02
follow the instructions in the readme file
to get set up with our list of birds.
0:04
Next, we'll be covering set operations.
0:09
Set operations deal with performing
comparisons on the contents of a single
0:12
set or across multiple sets of data.
0:17
The first one we'll use is pretty
self-explanatory, distinct.
0:21
It examines a single sequence and
returns only the distinct elements.
0:25
Let's select all
the colors from the birds,
0:31
birds.Select(b => b.Color).
0:37
So we'll get a list of strings, yep.
0:42
But it looks like we've got duplicates,
there's two reds.
0:45
When we use the distinct operator,
we should only get the distinct elements,
0:49
so let's try that again.
0:53
I'm gonna up arrow to get my last command,
0:55
and say .Distinct.
1:00
Now, it returns distinct colors.
1:03
The next four operators examine
the contents of two sequences at once.
1:06
The first one, except, returns
the difference between two sequences.
1:11
In other areas like database languages,
1:16
this operation might be
called a difference.
1:19
Here's our first sequence,
here's our second sequence.
1:23
Now, when the two sequences overlap,
the elements they have in common are in
1:26
the overlap area and the outer areas are
the elements they don't have in common.
1:31
When we use an except operation,
1:36
we end up with the elements in
the first sequence that don't overlap.
1:39
Let's try it out.
1:44
var colors = new List<string>.
1:46
We'll do "Pink", "Blue", and "Teal".
1:54
Then we'll call the except operator on
our list of colors, so colors.Except.
2:07
Then we'll do (birds.Select(b
2:14
=> b.Color).
2:21
So that should return all of
the colors in our birds list.
2:25
But let's go ahead and call .Distinct
anyway to remove duplicates.
2:30
So the parameter here inside the except
should be a list of bird colors.
2:36
Let's see what it returns.
2:43
Pink and teal.
2:46
So pink and
teal are not in our birds list as a color.
2:47
Our next operator, union,
takes two sequences and
2:53
returns the elements in both sets,
but removes duplicates.
2:57
Here's our first sequence,
here's our second sequence.
3:02
When we use a union operation,
we end up with the elements in all areas.
3:07
Let's try it out.
3:13
I'm gonna clear the console, but
3:14
we still have our colors list here,
and I'm gonna perform a union.
3:18
So first colors.Union and
then do the exact same thing,
3:23
(birds.Select(b =>
3:29
b.Color).Distinct()).
3:34
And we get all the colors both from our
colors list and the colors of our birds.
3:39
But you'll notice, the duplicates
are removed because our color
3:46
blue is both in our colors list and
in our birds list.
3:50
Let's check it out and
make sure birds.Where(b
3:56
=> b.Color == "Blue") and
there's a blue jay.
4:01
Our next operator intersect
takes two sequences and
4:09
returns the elements
that they have in common.
4:13
Here's our first sequence,
here's our second sequence.
4:17
When we us an intersect operation, we end
up with the elements in the overlap area.
4:22
Let's try it out.
4:29
I'm gonna clear the console again and
we've still got our colors.
4:30
So I'll call it on colors.Intersect and
4:37
we'll do the same thing as
with the other operators,
4:43
(b => b.Color).Distinct()), "Blue".
4:50
So blue is the only color that's
both in our colors list and
4:58
in our birds colors list.
5:02
There's one more operator
I wanna show you.
5:05
It's similar to the union operator, but
5:07
instead of removing the duplicates,
it returns everything.
5:10
colors.Concat.
5:14
So concat stands for
5:18
concatenation which means pushing
two things together into one.
5:20
And we'll do birds.,
same thing, (b => b.Color)).
5:25
So concat, our colors lists with
our birds, I see a typo, birds.
5:33
So there's all our colors and we've got
duplicates, two reds and two blues.
5:43
Now, we could even get the same results
from the union operator by calling
5:50
distinct after the concat operation
to remove the duplicates, right?
5:55
colors.Concat(birds.Select(b =>
5:59
b.Color)).Distinct.
6:07
Okay, and then we'll do that union again.
6:14
colors.Union(birds.Select(b =>
b.Color)) and
6:18
it returns the exact same thing.
6:26
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up