views:

45

answers:

2

I was having a discussion with a co-worker over whether it is better to use a Dictionary and repopulate it whenever a result set changes or to just use linq loop over all elements in the list each time.

We're trying to map a parent / child relationship, and I suggested using the ParentID as the dictionary key, and the dictionary value as the custom object.

We will usually be needing to iterate over all parent / children and run down the list.

Is there any reason why you wouldn't want to use a Dictionary in this circumstance? if so what would you use instead

+1  A: 

Performance wise, it's really going to depend on your specific application, so you'll have to test.

Toby
+1  A: 

If you really do need to iterate over the whole collection every time, there would not be much (if any) benefit to having a Dictionary. On the other hand, if you occasionally need to locate specific instances by their keys, Dictionary could be what you need.

Note that even if you are using a Dictionary, you can still just iterate through the list of Values when needed, for not too much more 'weight' than iterating through a normal List, but then you'd still have the Dictionary capabilities, if needed.

Andrew Barber