Click here to Skip to main content
13,660,776 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.2K views
66 downloads
13 bookmarked
Posted 17 May 2018
Licenced CPOL

C# Linq GroupBy ExtensionMethod Explained

, 17 May 2018
Rate this:
Please Sign up or sign in to vote.
This tip provides examples of how to use the different arguments of the Linq GroupBy extension method.

Introduction

I recently tried to lookup information on the Linq GroupBy, and found that it was very poorly explained. I hope that this tip helps people trying to use GroupBy.

Here is how GroupBy is defined in MSDN:

public static IEnumerable<IGrouping<TKey, TElement>> <code>GroupBy</code><TSource, TKey, TElement>(
 this IEnumerable<TSource> source,
 Func<TSource, TKey> keySelector,
 Func<TSource, TElement> elementSelector
)

Not very helpful, is it?

The Classes Used for Sample

For this tip, two class types are used. The main class, Department, has an IEnumerable property Items, which is of type Item. I did this because this is a complex case, and I think it provides better visibility on how to deal with more complex cases than a simple single level hierarchy:

public class Department
{
    public string Name { get; set; }
    public string DepartmentType { get; set; }
    public List<Item> Items { get; set; }
}

public class Item
{
    public string Name { get; set; }
    public string ItemType { get; set; }
}

Different Ways to Skin the Rabbit

The following all provide the same result, but do them in different ways using the GroupBy to obtain the same result:

var results = departments
    .GroupBy(o => o.DepartmentType)
    .Select(x => new
{
    Type = x.Key,
    ItemTypes = x.SelectMany(items => items.Items)
         .Select(item => item.ItemType).Distinct().OrderBy(k => k).ToList()
});

var results = departments
    .GroupBy(o => o.DepartmentType, o => o)
    .Select(x => new
{
    Type = x.Key,
    ItemTypes = x.SelectMany(items => items.Items)
         .Select(item => item.ItemType).Distinct().OrderBy(k => k).ToList()
});

var results = departments
    .GroupBy(o => o.DepartmentType, o => o.Items)
    .Select(x => new
    {
        Type = x.Key,
        ItemTypes = x.SelectMany(items => items)
         .Select(item => item.ItemType).Distinct().OrderBy(k => k).ToList()
    });

var results = departments.GroupBy(o => o.DepartmentType, o => o, (key, g) => new
{
    Type = key,
    ItemTypes = g.SelectMany(items => items.Items)
        .Select(item => item.ItemType).Distinct().OrderBy(k => k).ToList()
});

var result1 = departments.GroupBy(o => o.DepartmentType, o => o.Items, (key, g) => new
{
    Type = key,
    ItemTypes = g.SelectMany(item => item)
        .Select(item => item.ItemType).Distinct().OrderBy(k => k).ToList()
});

Hopefully, the code is self-explanatory.

Notes:

If you have any suggestions on how this could be improved, please contact me. There are a several people that have not given this 5 stars and would like to know how I could make it more useful.

Conclusion

Hopefully, these samples will quickly help you in understanding the GroupBy. I looked at a bunch of tips, samples, and articles that tried to explain the GroupBy. Hopefully, this will be more useful.

History

  • 2018-05-17: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180810.1 | Last Updated 17 May 2018
Article Copyright 2018 by Clifford Nelson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid