Click here to Skip to main content
Click here to Skip to main content
Go to top

Grouping a List on a String Property without LINQ

, 14 Oct 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Greg Olmstead
Founder SparkIT Solutions
Canada Canada
My formal education is in Network Engineering and Security Analysis, but I have been programming for about four years. My specialty is ASP.NET, C#, WCF, and WPF. I live in Ontario, Canada.
 
SparkIT Solutions provides customized software and hardware solutions for small to large businesses. Check us out at http://www.sparkits.ca

Comments and Discussions

 
AnswerThanks PinmemberYanZhiwei22-Apr-14 22:54 
GeneralMy vote of 3 [modified] PinmemberArchElf22-Oct-10 10:06 
GeneralLINQBridge PinmemberMember 257466515-Oct-10 9:26 
GeneralGood work. PinmemberKelvin Armstrong14-Oct-10 22:05 
GeneralSee also my article PinmemberQwertie27-Sep-10 10:40 
QuestionName Value? PinmemberAjay Vijayvargiya23-Sep-10 5:35 
AnswerRe: Name Value? PinmemberGreg Olmstead23-Sep-10 5:50 
GeneralNeeds more PinmvpMark Nischalke22-Sep-10 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 14 Oct 2010
Article Copyright 2010 by Greg Olmstead
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid