GroupBy and OrderBy Methods
Learn how to group and order the elements of a collection using GroupBy and OrderBy methods.
How to group elements of a collection
The GroupBy method groups the elements of a collection based on a grouping key. This method returns a collection of groups, or buckets, organized by that key.
For example, let’s group our movies by rating.
The GroupBy method receives as a parameter a delegate with a property to use as a key when grouping elements. In our previous example, we used the Rating and wrote movie => movie.Rating.
The result of GroupBy is a collection of groups or buckets. In our example, the type of the return collection was IEnumerable<IGrouping<float, Movie>>. That’s why we needed two foreach loops to print the movies in each group. One to print the ratings ...