1 00:00:01,340 --> 00:00:05,080 Our workspaces has all the new and improved classes for us. 2 00:00:05,080 --> 00:00:10,241 You can follow along in the example, 3 00:00:10,241 --> 00:00:16,734 just like we did before, with the same steps, 4 00:00:16,734 --> 00:00:24,396 csharp, LoadAssembly("BirdWatcher.dll"), 5 00:00:24,396 --> 00:00:27,893 and using BirdWatcher, 6 00:00:27,893 --> 00:00:32,222 then we'll load up our birds, 7 00:00:32,222 --> 00:00:38,400 var birds = BirdRepository.LoadBirds. 8 00:00:40,260 --> 00:00:44,220 Okay, let's explore our data a little by getting some record counts. 9 00:00:44,220 --> 00:00:50,200 Let's see how many birds we're dealing with here, birds.Count. 10 00:00:50,200 --> 00:00:52,230 That's a decent number. 11 00:00:52,230 --> 00:00:55,040 Now, how many sightings do we have? 12 00:00:55,040 --> 00:00:59,691 Each bird has a list of sightings, so 13 00:00:59,691 --> 00:01:07,945 we can perform a select on all the birds to get all the sightings and 14 00:01:07,945 --> 00:01:13,799 then call the count method, birds.Select, 15 00:01:13,799 --> 00:01:18,480 b goes to b.Sightings, and Count. 16 00:01:18,480 --> 00:01:19,950 Wait, that can't be right. 17 00:01:19,950 --> 00:01:23,892 There's no way that each bird only has one sighting. 18 00:01:23,892 --> 00:01:28,251 I forgot to use select many, the select method returned to us 19 00:01:28,251 --> 00:01:32,420 an innumerable of sighting sequences for each bird. 20 00:01:32,420 --> 00:01:36,592 So we just counted how many collections of sightings there are total, and 21 00:01:36,592 --> 00:01:38,420 not the sightings themselves. 22 00:01:38,420 --> 00:01:43,931 Let's try that again, this time using select many to 23 00:01:43,931 --> 00:01:48,953 flatten out the sightings into one collection, 24 00:01:48,953 --> 00:01:54,465 birds.SelectMany, where b goes to b.Sightings, 25 00:01:54,465 --> 00:01:57,420 and then we'll call count. 26 00:02:00,020 --> 00:02:01,340 Now that's better. 27 00:02:01,340 --> 00:02:03,680 A decent number for us to work with. 28 00:02:03,680 --> 00:02:08,080 Let's see, what's the average sighting count per bird in our data? 29 00:02:08,080 --> 00:02:10,430 Well, we can use the total number of sightings and 30 00:02:10,430 --> 00:02:14,150 then put it over the total number of birds, right? 31 00:02:16,730 --> 00:02:20,342 Nine, well, you know there's a better way we can do that, 32 00:02:20,342 --> 00:02:22,740 we can use the average method instead. 33 00:02:22,740 --> 00:02:27,870 So, since we want the average of the total count per bird, 34 00:02:27,870 --> 00:02:34,168 we should use select to get a sequence of the total counts, you follow? 35 00:02:34,168 --> 00:02:39,816 Select b goes to b.Sightings.Count, 36 00:02:39,816 --> 00:02:44,140 and then we'll call Average. 37 00:02:48,270 --> 00:02:51,580 That worked, and it looks like it was a little more accurate too. 38 00:02:51,580 --> 00:02:56,770 Well, now I'm wondering how many different countries we have bird sighting data in. 39 00:02:56,770 --> 00:03:00,220 Let's see, so to get to the country of each sighting, 40 00:03:00,220 --> 00:03:03,410 we'll need to access the place property of sighting. 41 00:03:03,410 --> 00:03:10,601 Well, you select many first to get a flat list of sightings, 42 00:03:10,601 --> 00:03:16,750 birds.SelectMany, b goes to b.Sightings. 43 00:03:16,750 --> 00:03:20,970 So, that'll give us a big collection of all the sightings. 44 00:03:21,970 --> 00:03:27,600 Then, we need to get each habitat on each sighting, not habitat, I think it's place. 45 00:03:29,110 --> 00:03:30,250 Let's check just to make sure. 46 00:03:32,210 --> 00:03:37,270 So on the sighting, class, yep, it's called place. 47 00:03:39,410 --> 00:03:44,479 So, we'll need to call a select, where s, 48 00:03:44,479 --> 00:03:50,918 I'll use s, since we're dealing with sightings now, 49 00:03:50,918 --> 00:03:55,850 goes to s.Place, and then .Country, so 50 00:03:55,850 --> 00:04:00,660 that should return all of our countries. 51 00:04:03,420 --> 00:04:06,202 Well, I got an extra space here. 52 00:04:06,202 --> 00:04:09,070 All right, now that we've got a giant list of country strings, 53 00:04:09,070 --> 00:04:12,750 we can use distinct to get a list of the distinct countries. 54 00:04:13,770 --> 00:04:18,270 So I'll press up to get my last command, and I'll call Distinct. 55 00:04:21,030 --> 00:04:22,730 All right, that's a little better. 56 00:04:22,730 --> 00:04:26,000 Now, let's see how many sightings are in each country. 57 00:04:26,000 --> 00:04:30,270 Usually when I hear the phrase, in each, or the word, by, 58 00:04:30,270 --> 00:04:35,450 as in count of sightings by country, I know that I'll need to use a grouping. 59 00:04:35,450 --> 00:04:40,845 Let's start out with all our sightings, 60 00:04:40,845 --> 00:04:47,013 birds.SelectMany, b goes to b.Sightings, 61 00:04:47,013 --> 00:04:52,717 then we'll need a GroupBy, so .GroupBy, 62 00:04:52,717 --> 00:04:57,957 and then we'll group by the country, so 63 00:04:57,957 --> 00:05:04,454 that's GroupBy, s goes to s.Place.Country. 64 00:05:06,020 --> 00:05:08,210 All right, let's see what that gives us. 65 00:05:08,210 --> 00:05:13,360 Wow [LAUGH], so we've got a bunch of groups of sighting objects, 66 00:05:13,360 --> 00:05:18,700 and the key should be countries, so now to get the final count by country, 67 00:05:18,700 --> 00:05:23,990 we'd need to perform a select, and then create an anonymously typed object. 68 00:05:23,990 --> 00:05:28,160 So, I'll tack on a Select method, 69 00:05:29,610 --> 00:05:33,950 Select, and I'll say group instead of a letter. 70 00:05:33,950 --> 00:05:38,613 We're gonna do an anonymous type here, so new, 71 00:05:38,613 --> 00:05:44,073 curly brace, and the country, should be the grp.Key, 72 00:05:44,073 --> 00:05:51,162 and then the Sightings is what we'll call the account, and grp.Count. 73 00:05:56,322 --> 00:06:01,130 Okay, let's see what I did there. 74 00:06:03,330 --> 00:06:05,410 Got an extra space again. 75 00:06:07,040 --> 00:06:07,570 Let's try that. 76 00:06:09,300 --> 00:06:14,380 Okay, so it looks like we've got, Canada's got 200, 77 00:06:14,380 --> 00:06:19,860 the US has 233, 200 in pretty much each country. 78 00:06:19,860 --> 00:06:23,080 Feel free to play around a little more and write some link queries for 79 00:06:23,080 --> 00:06:25,290 our bird data before we get to the next video.