Click here to Skip to main content
15,867,906 members
Articles / Programming Languages / C# 4.0

Grouping a List on a String Property without LINQ

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
14 Oct 2010CPOL2 min read 36.8K   252   12   8
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.

C#
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.

C#
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)


Written By
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 Pin
YanZhiwei22-Apr-14 22:54
YanZhiwei22-Apr-14 22:54 
GeneralMy vote of 3 [modified] Pin
ArchElf22-Oct-10 10:06
ArchElf22-Oct-10 10:06 
GeneralLINQBridge Pin
Member 257466515-Oct-10 9:26
Member 257466515-Oct-10 9:26 
GeneralGood work. Pin
Kelvin Armstrong14-Oct-10 22:05
Kelvin Armstrong14-Oct-10 22:05 
GeneralSee also my article Pin
Qwertie27-Sep-10 10:40
Qwertie27-Sep-10 10:40 
QuestionName Value? Pin
Ajay Vijayvargiya23-Sep-10 5:35
Ajay Vijayvargiya23-Sep-10 5:35 
AnswerRe: Name Value? Pin
Greg Olmstead23-Sep-10 5:50
Greg Olmstead23-Sep-10 5:50 
GeneralNeeds more Pin
Not Active22-Sep-10 7:31
mentorNot Active22-Sep-10 7:31 

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

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