Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Grouping a List on a String Property without LINQ

0.00/5 (No votes)
14 Oct 2010 1  
Grouping a List based on string property of the object in the list without LINQ

Introduction

Frequently in my line of work, I will work for a company that wants to use the .NET Framework, but seems to be stuck 1-2 years behind the current trend. For instance, some companies cannot move beyond .NET 3.0 for compatibility reasons, and/or Visual Studio 2005 for financial reasons. That means no LINQ, among other things.

Without LINQ, it can be a pain to perform the kind of operations on collections that LINQ makes easy. For instance, LINQ makes it very simple to extract a grouped collection of lists from a Generic List of Objects. Without it, one must use reflection to keep the grouping operation generic and not have to have knowledge of the objects inside the List.

This is a generic List class which inherits from List<T>, that allows grouping on a property of the objects contained in the list, without needing LINQ. This can help you perform grouping on Lists without needing to upgrade your version of the .NET Framework, if your company has such a restriction.

Currently, the List only supports grouping on a String property.

Background

This came about because we needed to extract a List of Lists by grouping on a property of the original List's objects, without using LINQ or anything above .NET 3.0. Currently only Strings can be grouped on, however this will be expanded soon to include any object.

Using the Code

To use the list, either instantiate it with its parameterless constructor, or pass an IEnumerable<T> into the constructor to initialize it with an existing List.

After the List is initialized and has content, call the GroupOnString method to obtain a List of Lists grouped by the specified Property.

List<MyCustomObject> list = GetMyList(); //This grabs me a list of my custom objects
GroupingList<MyCustomObject> groupinglist = new GroupingList<MyCustomObject>(list);
List<List<MyCustomObject>> mylist = groupinglist.GroupOnString("MyProperty");  

Now you have a List<List<MyCustomObject>>, in which each List<MyCustomObject> is grouped by the specified property.

The list also contains a method for retrieving a unique list of values for the property you specified as a List<String>, by calling the GetUniqueList method.

List<MyCustomObject> list = GetMyList(); 	//This grabs me a list of my custom objects
GroupingList<MyCustomObject> groupinglist = new GroupingList<MyCustomObject>(list);

List<String> uniqueList = groupinglist.GetUniqueList("MyProperty");

The result is a List<String> which contains a unique list of values for the given property.

How It Works

The class first searches through the list provided to it to gather a unique list of values of the property you instructed it to group on. For instance, if you wanted to group on the property "WarehouseName", the list would first gather all of the unique "WarehouseName" values.

Once it has this unique list, the list will then search through the list again and create a new List<T> for each value of "WarehouseName". It will then add all of the objects that have matching "WarehouseName"'s into the new list, and does this for each unique value of "WarehouseName". Once this is done, It returns a List<List<T>>, which is simply a collection of your newly grouped lists.

The class uses Reflection to do this, and therefore does not require the use of LINQ or anything above .NET 2.0.

History

  • 1.0 - Initial revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here