views:

424

answers:

3

I'm wondering what Data Structure people would recommend to do the following. I have a Class which has three main properties eg.

public class Example {

  public Object One { get; }
  public Object Two { get; }
  public Object Three { get; }
}

Another class contains a collection of these Objects and frequently needs to enumerate over these which I do mainly with LINQ. A lot of the time though I need to lookup/enumerate only a subset of these objects based mainly on the value of property One so what I'd like to do is store these in an efficient data structure based on that property. I could do something like the following:

Dictionary<Object,List<Example>>

But this strikes me as being very inefficient, I know I need some kind of Hash Table but having never used one before in C# I'm unsure of what there is to use.

Some other requirements/notes:

  • All the Objects are immutable and have fixed Hash Codes which are computed from the values the class gets instantiated with in the constructors
  • Must be able to store multiple items that have the same value (and thus Hash Code) for property One in the same 'slot' in the data structure
  • Must be able to freely add and remove Objects from the collection
+2  A: 

Indexed LINQ may be able to help you here. It provides an in-memory collection, but lets you attribute properties on your objects as Indexable so that it can create efficient queries against them.

Samuel Jack
seems good in theory but I can't get my code to compile if I define [Indexable()] on the relevant properties of the class I'll be storing
RobV
Sounds odd - what errors are you getting?
Samuel Jack
compile errors turns out to be that they removed the Indexable attribute in favour of defining IndexSpecifications
RobV
done some benchmarking with this and found it doesn't give any performance advantage (beyond a couple of ticks) even over a data set which is around the largest I'll perform a LINQ query over at any time
RobV
A: 

or HybridDictionary

Ada
It would be helpful to include a link to the MSDN documentation.
Samuel Jack
This doesn't really satisfy the second requirement of storing multiple items in one slot as I'll just end up with a Dictionary of Lists again
RobV
+1  A: 

PowerCollections (http://www.codeplex.com/PowerCollections) has a MultiDictionary container - perhaps you could try that?

Eamon Nerbonne
I've ended up going with a Hash Table class which works similarily to MultiDictionary since I've been playing with more benchmarking and find that this gives significant lookup performance improvement at the cost of minor load performance loss
RobV