I have an in-memory ‘table’ that might looks something like this:
Favorite# Name Profession --------- ---------- ------------------ 3 Names.Adam Profession.Baker 9 Names.Bob Profession.Teacher 7 Names.Carl Profession.Coder 7 Names.Dave Profession.Miner 5 Names.Fred Profession.Teacher
And what I want to do, is do quick and efficient lookups, using any of the 3 fields. In other words, I want:
myTable[3]andmyTable[Names.Adam]andmyTable[Professions.Baker]to all return{3,Names.Adam,Profession.Baker}myTable[Profession.Teacher]to return both{9,Names.Bob,Profession.Teacher}and{5,Names.Fred,Profession.Teacher}.
The table is built during runtime, according to the actions of the user, and cannot be stored in a database since it is used in sections in which database connectivity cannot be guaranteed.
Right now, I ‘simply’ (hah!) store this using 3 uber-Dictionaries, each keyed using one of the columns (FavoriteNumber, Name, Profession), and each value in the uber-Dictionaries holding 2 Dictionaries which are themselves keyed with each of the remaining columns (so the values in the ‘Name’ uber-dictionary are of the type Dictionary<FavoriteNumber,Profession[]> and Dictionary<Profession, FavoriteNumber[]>
This requires 2 lookups in 2 Dictionaries, and another traverse of an array (which usually holds 1 or 2 elements.)
Can anyone suggest a better way to do this? I don’t mind spending extra memory, since the table is likely to be small (no more than 20 entries) but I’m willing to sacrifice a little CPU to make it more readily maintainable code…
Not really however using a dictionary, but if you create a collection of classes like this
you can use LINQ to search the collections.
or if you prefer the normal LINQ syntax
Each of these
selectedPeoplevariables will be typed asIEnumerable<Person>and you can use a loop to search through them.