Bird Search In Action7:45 with Carling Kirk
We'll use the new Bird Search Extension to find the bird I saw.
We'll need to add the using directives for system.collections.generic and 0:00 system.link to this file. 0:05 Using System.Collections.Generic and using System. 0:09 Link. 0:20 Let's put our search feature to work and 0:23 try to figure out what the bird was that I saw the other day. 0:25 We can write a little console application that will cycle through each page of 0:29 the results until we press a certain key. 0:33 So let's hop over to the program.cs file. 0:36 First, let's create our search parameters. 0:41 var searchParameters= new BirdSearch. 0:45 So we'll use object initialization here. 0:52 And so the bird was Size = medium and 0:59 The country was the United States and 1:06 the colors we have to give 1:13 it a new list of string and 1:18 the colors were White, 1:23 Brown and Black. 1:27 Okay, and then the page will give it zero to start out. 1:32 And the page size will give it, what did I say earlier a five, all right. 1:40 So we've got our search parameters. 1:49 Let's write something to the console so they know what's going on. 1:53 Console.WriteLine Type any key to begin search. 1:56 Kinda like a go button, right? 2:03 All right, now we're gonna need a loop to loop through our results. 2:09 While Console.ReadKey, 2:13 so that'll read only one key at the console. 2:18 And it has a property called KeyChar. 2:22 And if that is not equal to q, we'll say. 2:26 Okay. 2:33 So this loop is going to keep looping and 2:35 reading a key from a console until somebody presses a q. 2:38 First, let's write out something to the console. 2:44 I'm going to use a new feature of c sharp instead of using 2:48 string.format like we have in previous videos. 2:53 I'm gonna use a dollar sign, then a string quote. 2:56 Inside here, I'm gonna say Page colon, and open close curly braces. 3:01 So when I put this dollar sign right here, inside the curly braces, 3:10 I can put in code. 3:14 So we'll use our searchParameters .Page. 3:16 So we're going to let whoever's at the console know what page they're on. 3:21 Then, I think we need to use our search on birds but 3:28 we don't have a list of birds so we'll create a new var 3:33 birds one last time = BirdRepository.LoadBirds. 3:40 Okay, and now we can call our search method .search and 3:49 we'll pass it the searchParameters search parameters And 3:54 then will just well call ToList put it in a list and 4:00 there's an other method I wanna show you called ForEach. 4:05 And what this does we can pass it on lambda. 4:12 goes to and what that does is run a piece of code for every element in the list. 4:16 So for each b in birds, 4:24 let's write out to the console, console.WriteLine. 4:28 And we'll do that magic string mashing thing again. 4:32 We'll start out with a dollar sign. 4:38 I like to go ahead and quote, quote end my method just so 4:41 I don't have any syntax errors. 4:44 So in here we're going to write the name of the bird Common Name 4:46 equals and then we need our curly braces. 4:53 So that's b.CommonName because our b Is our input parameter for a lambda. 4:55 All right, and we need to close out the for each method. 5:06 So while this is looping, it's not gonna change the page. 5:14 We need to change the page after every time we print them out to the screen. 5:18 So let's search parameters every time we loop through, we'll add to the page. 5:23 Page, plus plus. 5:32 So, we'll add one to the page property. 5:32 Okay. 5:36 So I think we can go ahead and compile this guy. 5:38 So we're going to compile on the console with mcs. 5:41 All of our files with star.cs. 5:44 Then we're gonna specify the out argument and we're gonna give it Program.exe. 5:47 So that it'll compile all our files and give us an executable called 5:56 Program.exe, we've got an error. 6:00 Bird search extension.cs. 6:05 Let's go check that out. 6:07 Line 22 oops looks like we've got a couple of typos here 6:11 where s.size and search should have been an a in it. 6:16 Did you catch that before I did? 6:22 All right, we'll save that file and we'll try it again. 6:23 Okay. 6:30 BirdSearchExtension, the name page does not exist. 6:33 That's line 29. 6:38 Ha. 6:40 I put in just random variable names here. 6:41 This should be search.Page and 6:48 this should be search.PageSize, 6:54 okay, and search.PageSize. 7:02 All right, let's try to compile again. 7:09 All right great, so now we can run 7:15 this with mono Program.exe. 7:20 Okay, type any key to begin search. 7:24 All right, there's our first page could be any of those. 7:28 [LAUGH] All right. 7:31 2, 3. It looks like we've run out of 7:36 bird results. 7:38 Now, I'll press q to exit. 7:40 Awesome. 7:43
You need to sign up for Treehouse in order to download course files.Sign up