tags:

views:

48

answers:

1

Hello,

I use Dictionary as a heterogeneous container:

Dictionary<string, Object> _Table;

It basically maps some description to int/double/string/ISerialiazable/etc types.

I want to efficiently calculate number of elements of each type, and then only print the elements of that type -- in SQL, I would group by type, then print count and all elements for each type respectively.

I am not using LINQ, as I need to support this in .net 2.0. I certianly encourage linq-like solutions for educational purposes, but please keep in mind that I will only use a code compatible with .NET 2.0

I currently have a workaround, whereby I use Dictionaries for each types, and then iterate over all dictionaries.

Thanks....

+2  A: 

EDIT: You wanted to print the objects as well:

Dictionary<Type, List<object>> numTypes = new Dictionary<Type, List<object>>();

foreach(KeyValuePair<string, object> pair in _Table){
    Type type = object.GetType();
    if (!numTypes.ContainsKey(type)){
     numTypes[type] = new List<object>();
     }

     numTypes[type].Add(object);
}
DavidN
second dictionary? looks like a redundancy.