Check out a free preview of the full C# and .NET Basics course
The "LINQ: GroupBy & SelectMany" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:
Spencer demonstrates how to use the Distinct, GroupBy, SelectMany, and Join methods in C#. He shows how to use the Distinct method to get a unique list of items, the GroupBy method to group items together based on a specific value, the SelectMany method to flatten multiple collections into one, and briefly mentions the Join method for combining two collections based on a common value.
Transcript from the "LINQ: GroupBy & SelectMany" Lesson
[00:00:00]
>> Spencer Schneidenbach: Lastly, I want to talk about, I showed distinct in the context of anonymous types. You can also do them on values like string or int. You can do those on those kinds of values to get a distinct list of items. There is a group by method which is really neat.
[00:00:21]
So given that you have a bunch of employees, you can group them together. Let's group them real quick by var grouped. It would be employees.GroupBy, and we'll just say by e.FirstName because we know that there are multiple employees with those first names, put that return statement down so it only prints what I want.
[00:00:44]
And then we can spin through this group var employees in group. And what grouped, is actually an IEnumerable of another IEnumerable especially called IGrouping, so we can print to the console
>> Spencer Schneidenbach: right line. We can say emps.Key that key is the value that we've grouped on. So what we can say is we're.
[00:01:25]
>> Spencer Schneidenbach: We can print that key right here, which it would take the people that had the same name and group them together. So what you'd end up with is a collection of two Snapes, another collection of Spencer, Micah, and Grant. And if you look through you, as I mentioned this is also an IEnumerable so you can perform it for each on that,
>> Spencer Schneidenbach: And then Console.WriteLine.
[00:01:51]
>> Spencer Schneidenbach: Last name,
>> Spencer Schneidenbach: Is emp.LastName. So that's a way you can actually do some kind of grouping operations, which are really useful. And as you can see, these Spencer employees, the last name George, there's one there are two Snape employees with the first name Snape that have the last name George.
[00:02:14]
And we can change this to Larry and show that even more explicitly .NET run. You can see that there are two employees named Snape and they each have a last name of George and Larry respectively. And then we have Micah employees and then Grant. And so that's a way that you can use to group together values in a collection, which is incredibly useful, occasionally.
[00:02:42]
Last link method that I will mention is if you have, for say, a list of managers, and each of those managers has an employee under them, you may want to select all of the values of each of these employee arrays and then flatten them out into one single array.
[00:02:57]
So you might wanna combine this array and this array, but you only want one array. So you can use what's called SelectMany, and that will take those collections and flatten them out into one such that the the names that will be printed to the console here are Mike, Jane, Peter, and Chris.
[00:03:20]
So we've selected that collection here, and then with SelectMany and then basically it just takes those two collections and combines them together. Honorable mention, to join something that very rarely gets used because of the nature of some of the libraries that we use, which we'll touch on in the ASP.NET Core course.
[00:03:39]
But join is a way to take two collections and join them together on an equal value.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops