Click here to Skip to main content
13,594,279 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 23 Sep 2010
Licenced CPOL

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


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.


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.


  • 1.0 - Initial revision


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


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

You may also be interested in...


Comments and Discussions

AnswerThanks Pin
YanZhiwei22-Apr-14 22:54
memberYanZhiwei22-Apr-14 22:54 
GeneralMy vote of 3 [modified] Pin
ArchElf22-Oct-10 10:06
memberArchElf22-Oct-10 10:06 
GeneralLINQBridge Pin
Member 257466515-Oct-10 9:26
memberMember 257466515-Oct-10 9:26 
GeneralGood work. Pin
Kelvin Armstrong14-Oct-10 22:05
memberKelvin Armstrong14-Oct-10 22:05 
GeneralSee also my article Pin
Qwertie27-Sep-10 10:40
memberQwertie27-Sep-10 10:40 
QuestionName Value? Pin
Ajay Vijayvargiya23-Sep-10 5:35
memberAjay Vijayvargiya23-Sep-10 5:35 
AnswerRe: Name Value? Pin
Greg Olmstead23-Sep-10 5:50
memberGreg Olmstead23-Sep-10 5:50 
GeneralNeeds more Pin
Mark Nischalke22-Sep-10 7:31
mvpMark Nischalke22-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180618.1 | Last Updated 14 Oct 2010
Article Copyright 2010 by Greg Olmstead
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid