I have a List<Thing> things
, where a number of Thing
s need to be frequently retrieved by looking up a combination of two variables T1 f1
and T2 f2
, which are value types. They way I do that now is simply things.Where(t => t.Field1 == f1 && t.Field2 == f2)
. However, I do extremely many of those lookups frequently, and need a more effective method.
Fortunately, things
does not need to have elements removed or added, so I thought of parsing the list on construction and add to a Dictionary<T1, Lookup<T2, Thing>>
. However, this feels messy, especially with the added parsing. And it gets really hairy if I need to lookup even more fields. Three fields would look like Dictionary<T1, Dictionary<T2, Lookup<T3, Thing>>>
.
My next thought was to make a Lookup<Tuple<T1,T2,T3,...>,Thing>
. But in this case, I am not sure whether the keys will actually work because Tuple is a reference type.
Even if I make a Lookup<ValueType<T1,T2,T3,...>,Thing> things
, the lookup statement will be something like things[new ValueType<T1,T2,T3,...>(f1, f2, f3, ...)]
which is pretty ugly (and I am still not sure whether I could trust those keys).
Is there a more elegant solution to this which keeps the performance benefits of a hashtable and where I could simply type something like IEnumerable<Thing> found = things[f1, f2, f3, ...];
?