I am in need of a generic collection that is able to store objects and query them in O(1) time based on their members. Consider the following class:

public class MyObject
  public readonly int Id;
  public string Name { get; set; }
  public double Value { get; set; }

I would like to store MyObject instances in the collection and be able to query objects by field/property using LINQ with (expected) O(1) lookup. Essentially, the values of the members would be keys to lists of matching objects.

An example usage might be:

var uniquePerson = collection.Query( x => x.Id == 10 ).First();
var peopleNamedTom = collection.Query( x => x.Name == "Tom" );

Does a collection like this exist? I'm guessing not, and will have to implement it myself, and it will involve a lot of reflection / dynamic IL. It would be much easier to just write a specialized collection that uses dictionaries for each property, but my goal is to have it as generic and reusable as possible.

0 Answers