I have a collection with millions of items of Generic type T. Assume this list never changes. I want to perform many types of searches with subsets of fields of type T. Some with only 1 field and others with 2 or more fields. Each search almost always returns more than 1 result.

This answer is very close to what I want but it only works for searches with only one field:


1 Answer 1


Actually, you can use the code just like in link you posted. And call it like this:

  var fc = new FastCollection<Product>(products);

  fc.AddIndex(item => new { UserName = item.UserName, Title = item.Title});

  string userName = "...";
  string title = "...";

  // finds items where UserName == userName and Title == title
  var items = 
        item => new { UserName = item.UserName, Title = item.Title}, 
                new { UserName = userName     , Title = title })

This code makes use of anonymous types to create a single "property" that is aggregate of multiple properties.

This also reminds me how indexes are done in RavenDB.

  • Yes that seems to be what I really want! Thanks! The catch here is that I always have to add the index before I perform the search.. if I have 10 different searches I have to add 10 different indexes.. meh.
    – deeproute
    Jan 15, 2019 at 15:52
  • 1
    @deeproute Well, you can change the code of the FastCollection, so that it adds new index if it doesn't find one, instead of falling back to Where. I actually assumed it was doing just that before I double-checked the code.
    – Euphoric
    Jan 15, 2019 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.