1 00:00:00,790 --> 00:00:04,240 In this video we'll be covering aggregate operators. 2 00:00:04,240 --> 00:00:06,650 I've still got my birds list in the REPL here. 3 00:00:06,650 --> 00:00:08,520 If you need to pause the video and 4 00:00:08,520 --> 00:00:12,080 follow the instructions in the readme file to get set up with our list of birds. 5 00:00:13,960 --> 00:00:17,690 Remember when we use the group operator and queries syntax. 6 00:00:17,690 --> 00:00:20,180 We haven't touched on it using method syntax. 7 00:00:20,180 --> 00:00:22,770 Let's review how to do that real quick. 8 00:00:22,770 --> 00:00:26,083 With query syntax we group birds by color like this. 9 00:00:26,083 --> 00:00:31,370 From b in birds, 10 00:00:31,370 --> 00:00:38,420 group b by b.Color. 11 00:00:38,420 --> 00:00:41,480 In method syntax, here's how we do it. 12 00:00:41,480 --> 00:00:44,692 Let's group our birds with just the group by operator first, 13 00:00:44,692 --> 00:00:49,760 birds.group by 14 00:00:49,760 --> 00:00:54,770 b goes to b.Color. 15 00:00:54,770 --> 00:00:59,170 Let's keep using the group by method as we venture into a new set of link operators 16 00:00:59,170 --> 00:01:00,620 aggregates. 17 00:01:00,620 --> 00:01:05,600 Aggregates help us to perform analysis operations on a dataset, like sum, 18 00:01:05,600 --> 00:01:07,220 count or average. 19 00:01:07,220 --> 00:01:09,410 We used the count operator briefly before. 20 00:01:10,840 --> 00:01:13,295 Let's get a count of birds for each color group. 21 00:01:13,295 --> 00:01:18,016 birds.GroupBy b goes to 22 00:01:18,016 --> 00:01:23,635 b.Color.Select g goes to, 23 00:01:23,635 --> 00:01:27,906 we'll do an anonymous 24 00:01:27,906 --> 00:01:32,850 type new Color = g.Key, 25 00:01:32,850 --> 00:01:37,571 Count equals g.count, 26 00:01:37,571 --> 00:01:41,392 close curly brace, 27 00:01:41,392 --> 00:01:47,260 close the method, and enter. 28 00:01:48,510 --> 00:01:51,460 So there are bird counts for each color. 29 00:01:51,460 --> 00:01:56,220 Notice that I used another letter g for the lambda parameter in the select method. 30 00:01:56,220 --> 00:01:59,760 I could have used a b, but I did that on purpose so that it's easy for 31 00:01:59,760 --> 00:02:03,140 me to differentiate that it's not just a plain bird. 32 00:02:03,140 --> 00:02:07,440 The objects in that are enumerable, are now groups of birds, 33 00:02:07,440 --> 00:02:10,592 let's try another aggregate operator sum. 34 00:02:10,592 --> 00:02:15,420 We can get the total number of sightings for 35 00:02:15,420 --> 00:02:18,505 all the birds birds.Sum, 36 00:02:18,505 --> 00:02:23,480 b goes to b.Sightings which is an integer. 37 00:02:24,840 --> 00:02:27,320 We can't just call Sum without a parameter. 38 00:02:27,320 --> 00:02:30,230 We need to tell it what property to sum up. 39 00:02:30,230 --> 00:02:34,003 Now let's get a sum of the sightings in each color group. 40 00:02:34,003 --> 00:02:38,698 birds.Groupby b 41 00:02:38,698 --> 00:02:44,019 goes to b.Color and 42 00:02:44,019 --> 00:02:49,653 Select, g goes to new 43 00:02:49,653 --> 00:02:54,661 Color = g.key and 44 00:02:54,661 --> 00:03:01,870 Sightings equals g.Sum. 45 00:03:01,870 --> 00:03:07,660 B goes to b.Sightings curly brace and 46 00:03:07,660 --> 00:03:10,310 then another close parenthesis. 47 00:03:12,240 --> 00:03:15,950 We can call the some method on the sightings collection in that grouping 48 00:03:15,950 --> 00:03:17,810 because it's also an innumerable. 49 00:03:19,080 --> 00:03:22,095 The other aggregates are used in the same way as some. 50 00:03:22,095 --> 00:03:29,144 birds.Average, b goes to b.Sightings. 51 00:03:31,449 --> 00:03:37,643 4 is the average, let's get the the min, 52 00:03:37,643 --> 00:03:43,000 birds.Min b goes to b.Sightings. 53 00:03:43,000 --> 00:03:46,458 Minimum of zero and one more, max. 54 00:03:46,458 --> 00:03:53,042 Max(b => b.sightings) and 55 00:03:53,042 --> 00:03:56,460 11 is our max. 56 00:03:58,280 --> 00:03:59,580 Check out the teachers notes for 57 00:03:59,580 --> 00:04:03,950 documentation on a couple more aggregate operators that are used less often. 58 00:04:03,950 --> 00:04:05,260 Long count and aggregate.