1 00:00:00,600 --> 00:00:02,440 Let's get back to LINQ queries. 2 00:00:02,440 --> 00:00:06,200 I'll need to load our list of birds again, but if you've already got yours 3 00:00:06,200 --> 00:00:09,128 in your console from the last video, you won't need to do this. 4 00:00:09,128 --> 00:00:14,549 csharp, LoadAssembly 5 00:00:14,549 --> 00:00:21,601 ("BirdWatcher.dll"). 6 00:00:21,601 --> 00:00:25,565 using Birdwatcher; and var, 7 00:00:25,565 --> 00:00:30,747 we're gonna use the var key word again, 8 00:00:30,747 --> 00:00:35,792 = BirdRepository.LoadBirds(). 9 00:00:35,792 --> 00:00:39,570 Okay, we've got a lot of birds in our birds list. 10 00:00:39,570 --> 00:00:42,230 Let's get them in some kind of order. 11 00:00:42,230 --> 00:00:45,298 To do that, we'll use the order by keyword. 12 00:00:45,298 --> 00:00:49,721 From B in birds 13 00:00:49,721 --> 00:00:55,623 orderby b.Name. 14 00:00:55,623 --> 00:01:00,061 Select b.Name. 15 00:01:00,061 --> 00:01:04,765 If you want to get the order reversed, 16 00:01:04,765 --> 00:01:12,409 you can use the word descending after the property like this, 17 00:01:12,409 --> 00:01:20,207 from b in birds orderby b.Name descending, select b.Name. 18 00:01:21,920 --> 00:01:24,130 If we want to order by multiple properties, 19 00:01:24,130 --> 00:01:25,410 we can separate them with a comma. 20 00:01:26,680 --> 00:01:32,229 So to get all birds ordered by color but 21 00:01:32,229 --> 00:01:37,259 then sightings in reverse order, 22 00:01:37,259 --> 00:01:44,717 we could write, from b in birds order by b.Color, 23 00:01:44,717 --> 00:01:52,193 b.Sightings descending, then select b.Name. 24 00:01:52,193 --> 00:01:56,140 Well, that just gives us the names. 25 00:01:56,140 --> 00:01:59,920 Let's use anonymous types again and get a list of names and sightings. 26 00:02:01,110 --> 00:02:07,850 Use the up arrow key to get some lines from 27 00:02:07,850 --> 00:02:13,005 b in birds orderby b.Color and 28 00:02:13,005 --> 00:02:17,764 b.Sightings descending, 29 00:02:17,764 --> 00:02:24,519 select new { b.Name, B.Sightings. 30 00:02:27,593 --> 00:02:32,249 So now you can see that our bird colors are in ascending order with Crow, 31 00:02:32,249 --> 00:02:35,586 that's black first, and our red birds, Robin and 32 00:02:35,586 --> 00:02:39,560 Cardinal, are in descending order according by sighting. 33 00:02:39,560 --> 00:02:41,080 So five and then three. 34 00:02:43,020 --> 00:02:44,850 Now let's talk about grouping. 35 00:02:44,850 --> 00:02:47,800 Grouping is often used when you need to organize a collection by 36 00:02:47,800 --> 00:02:49,580 a common attribute. 37 00:02:49,580 --> 00:02:51,110 It's kind of like filing. 38 00:02:51,110 --> 00:02:54,380 You need to put all the paperwork for one person into the same folder so 39 00:02:54,380 --> 00:02:55,530 it's easier to access. 40 00:02:56,700 --> 00:02:59,690 There are two new clauses we'll need, group and into. 41 00:03:00,810 --> 00:03:05,720 When you group a query result, you get a new sequence of type I grouping. 42 00:03:05,720 --> 00:03:09,830 Let's start with a grouping by a string, how about color? 43 00:03:11,200 --> 00:03:16,600 var birdsByColor 44 00:03:16,600 --> 00:03:21,640 = from b in birds 45 00:03:21,640 --> 00:03:28,130 group b by b.Color. 46 00:03:28,130 --> 00:03:30,860 Notice that I didn't use the select clause. 47 00:03:30,860 --> 00:03:32,488 Let's see what the type is. 48 00:03:32,488 --> 00:03:38,577 birdsByColor.GetType. 49 00:03:38,577 --> 00:03:43,084 Okay, so it's a group to numerable which means that instead of one set of birds, 50 00:03:43,084 --> 00:03:45,950 we now have different sets of birds for each color. 51 00:03:47,100 --> 00:03:49,530 Each grouping has a key, the color, and 52 00:03:49,530 --> 00:03:51,560 then the collection of birds that have that color. 53 00:03:52,620 --> 00:03:57,205 It's most often used with aggregations like as we've seen the count method. 54 00:03:57,205 --> 00:04:02,168 So we can access the collection of birds by key like this. 55 00:04:02,168 --> 00:04:09,609 foreach( var bird in birdsByColor) 56 00:04:12,806 --> 00:04:18,736 Console.WriteLine (bird.Key), 57 00:04:18,736 --> 00:04:22,332 which should be color, 58 00:04:25,645 --> 00:04:32,929 and bird.Count. 59 00:04:32,929 --> 00:04:36,930 So there's our birds, grouped by color, and how many birds are in each group. 60 00:04:38,090 --> 00:04:41,540 We can also use the into keyword in our query. 61 00:04:41,540 --> 00:04:45,840 It's helpful because it lets us use the group result in a where clause. 62 00:04:45,840 --> 00:04:47,402 Let's try that out. 63 00:04:47,402 --> 00:04:52,967 From b in birds group b 64 00:04:52,967 --> 00:04:59,774 by b.Color into whoops. 65 00:04:59,774 --> 00:05:04,074 Let's try that again, 66 00:05:04,074 --> 00:05:08,374 from b in birds group b by 67 00:05:08,374 --> 00:05:14,420 b.Color into birdsByColor. 68 00:05:14,420 --> 00:05:18,409 So it's actually a new range variable that we can use in our where class. 69 00:05:20,510 --> 00:05:26,164 where birds by color.Count > 1, 70 00:05:26,164 --> 00:05:33,138 select new { Color = birdsByColor.Key, 71 00:05:33,138 --> 00:05:40,677 and Count = birdsByColor.Count() }; and 72 00:05:40,677 --> 00:05:45,953 now we have two anonymously typed 73 00:05:45,953 --> 00:05:51,040 objects one with the color red, 74 00:05:51,040 --> 00:05:56,710 and the count of birds that are red. 75 00:05:56,710 --> 00:05:59,421 And another with the color white and the count of birds that are white. 76 00:06:00,535 --> 00:06:03,680 We'll be getting into some more grouping later when we talk about aggregates.