I have a Cache wrapper class that I use, which provides type safety and segmenting and other nice things.
I want to make it prevent me from shooting myself in the foot by caching an un-materialized LINQ query and only accept lists/collections…
Is there a way to detect if an IEnumerable is a LINQ query?
Maybe I’m answering my own question and should throw an exception when T is IEnumerable but not ICollection.
I would suggest just wrapping the
IEnumerable<T>within your own collection, “materializing” it yourself.This will provide full safety and more consistency, at the expense of potentially generating another collection instance and copying the references over.
You could always do a check for
ICollection<T>and not regenerate, or similar, but there are still advantages to copying the contents into your own list. One major one is that you then control the one and only instance of the collection – you don’t have to worry about another object adding or removing items (which may or may not be an issue, but is likely problematic for segmenting).