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
We'll write a custom extension method to implement a search feature on our Bird Watcher data set.
This video doesn't have any notes.
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
I saw a bird recently
that I couldn't identify.
0:00
I've got some clues though.
0:03
It was blue, brown, and black,
and it was medium size.
0:05
And I saw the bird in the United States.
0:09
We can build a search feature for
our bird data so
0:12
that we can find out
what bird it was I saw.
0:15
Think about a typical
advanced search scenario.
0:18
There's probably multiple text boxes
with different labels on them.
0:21
Maybe a drop down list for size that gives
options for tiny, small, medium, or large.
0:25
The text boxes are for string inputs,
like bird name, colors, and location.
0:31
And then there's probably a Go
button to initiate the search, and
0:37
then it should return a list of birds
that match the search parameters.
0:40
And probably not all of them at once,
but ten at a time.
0:44
And we need to click next or
previous to browse the results, and
0:47
sometimes there's an option to
view more than 10, like 50 or 100.
0:51
Let's check out how we would implement
that search feature using link and
0:56
our bird data.
0:59
Okay, we're back in Workspaces.
1:01
Let's create a new file
called BirdSearchExtension.
1:04
New File > BirdSearchExtension.cs.
1:07
Then let's put in the namespace,
which is Birdwatcher,
1:18
and we can create a new
class called bird search,
1:24
Public class BirdSearch.
1:29
So itβs duty will be to hold
the search data will pass into our yet
1:36
to be implemented search feature.
1:40
Letβs give it some properties.
1:42
Public string CommonName, get set.
1:44
Public List of strings for
1:50
the colors, so
1:56
list of strings, color,
1:59
get set and a string again for
2:05
Country get set.
2:11
And public string Size,
2:15
that's the size of the bird, get set.
2:19
And then our page and page size,
2:26
so we'll do a public int Page for
2:31
page number, and
2:36
a public int PageSize get set.
2:39
Okay, now, remember that the link
2:45
is just a bunch of extension methods
tacked onto the i numeral interface.
2:48
We can write our own extension
method to search for birds, so
2:53
we'll need a new static class in here.
2:57
Let's call it BirdSearchExtension.
3:00
Public static class,
3:03
BirdSearchExtension.
3:07
And so inside this class, our extension
method will also need to be static,
3:14
so public static and let's call it search,
and it'll return an i innumerable of bird.
3:20
IEnumerable of Bird, and
3:28
Search for the name of the method.
3:31
So now, since this is an extension method,
the first parameter will be the object
3:37
from which it's called, which is also
going to be an IEnumerable of bird.
3:42
And we need to use the this modifier to
indicate that it's an extension method,
3:47
this IEnumerable of Bird.
3:51
And we'll call it source if you
remember from the documentation,
4:00
that's how Microsoft did it.
4:03
And then we'll pass in for
our second parameter our BirdSearch,
4:05
and we'll call that search.
4:11
Okay, let's see if we can do our
entire search in one link expression.
4:16
That'll start out with a return,
and then from the source.
4:24
We can start out by
filtering with common name,
4:30
return source.Where I'll use s for
4:34
source, so s is an element
inside the source enumerable.
4:40
S goes to s.CommonName == search,
4:46
which is our bird search, .CommonName.
4:51
What about if someone entered a partial
name like the first three letters,
4:59
we should use the string method
contains to return on a partial match.
5:03
So let's redo that.
5:08
Where s.CommonName.contains
5:12
search.CommonName.
5:18
That way, if I entered in S-P-A,
it would return sparrows.
5:24
Well, what if somebody left the field for
name blank?
5:29
When we search for my bird,
we don't know what the name is, so
5:34
we'll need to check if the search
common name property is null first.
5:37
So where S goes to search.CommonName
5:44
== null or it has a match.
5:50
That looks good.
5:55
Next let's write another where clause for
the country.
5:57
I like to line up my link
methods together like this.
6:01
Where s goes to and
we'll check also if the country's null,
6:05
so search.Country == null or s.
6:13
Now, the country property is
actually not on the bird itself, but
6:18
it's on the habitat, and
let's go check out that class real quick.
6:22
Here we've got a list of
places as habitats, so
6:29
it's got a list of habitats
which are type place.
6:34
So let's check out the Place class and
it's gotta string of countries,
6:40
so we're gonna have to use
the any operator here.
6:45
So where the bird,
which is the S its habitats,
6:48
are there any of those?
6:54
We use h here for habitat,
h goes to h.Country.
6:56
We'll do a partial match on that too,
contains search.Country.
7:02
Okay, that looks good.
7:11
Now, let's do our size
parameter where S goes to.
7:14
We'll also check and
make sure it's not null.
7:21
Or this time I think
we'll do an exact match.
7:26
B.size ==
7:31
search.Size.
7:35
Okay, next we need to
narrow down by colors.
7:40
This is gonna be a little trickier because
we've got multiple color fields on our
7:42
bird class and the bird search variable
has a list of possible colours.
7:46
We'll start out with primary colour.
7:51
If any of our search colors
is the primary color,
7:54
we'll want to include
that bird in the result.
7:57
Ha, I just saw the operator we should use,
any.
8:00
But since we want to return birds,
if the colors match any of the primary,
8:02
secondary or tertiary colours,
8:07
we'll want to put all of those
conditions within the same where clause.
8:09
So .Where(s goes to, we'll start
8:17
out with the search.Colors if any
8:22
of those where c, we'll use c for
8:27
color goes to c == s.PrimaryColor.
8:32
All right, but
we won't close the where just then,
8:38
we're gonna put in or
because if it's the primary color or
8:44
the secondary color, we wanna return both.
8:51
So S goes to search.Colors.Any
where c goes to
8:56
same deal == SecondaryColor, okay.
9:01
Missed my s or so now we have to figure
9:05
out how to handle the tertiary colors.
9:10
Since we need to compare a list of
colors with another list of colors,
9:16
it sounds like we need to use a join.
9:20
Let's try that out.
9:23
Ope, looks like I've specified that too
many times, good thing I caught that.
9:25
Okay, so we don't need the s goes to.
9:30
We can start right there and
say where the search.Colors,
9:33
then we'll join the list of
colors from the search parameters
9:38
to s.TeriaryColors.
9:44
And we'll say sc for
search color goes to sc, and then
9:48
tc for tertiary colors goes to tc,
those are just a list of strings.
9:56
So we don't need to use any properties for
the key selectors.
10:01
Now, for our result,
10:05
both parameters sc and tc goes to sc.
10:09
So when we join the search colors and
10:17
the tertiary colors together, if there's a
match, it will return the matching color.
10:19
And all we care about is if there's at
least one, so we can tack on the any
10:24
operator to return a boolean
if that's the case, .Any.
10:29
Okay, so
all of our colors are in our filter.
10:35
Finally, we need to deal with the page and
page size perimeters.
10:40
Do you remember the operators skip and
take?
10:45
Skip will skip a number of
elements in the sequence and
10:48
take will specify how
many elements to return.
10:51
So .Skip page
10:54
* pageSize.
11:00
So we're gonna start out on page zero,
and when we use a page size of say five,
11:05
zero times five is zero, so
it's not gonna skip any of the elements.
11:11
But once we advance to page one, page
it would be one, pageSize will be five.
11:16
It'll skip ahead five elements and
that'll bring us to the next page.
11:21
Then, our final take, and
that'll just be the pageSize.
11:27
So if pageSize is five,
then we'll take five elements.
11:32
And we did it, we put all of our
search logic into one link expression.
11:37
Awesome job.
11:42
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