I have a large collection of objects
public class Restriction
{
// which days this restriction applies to
public DateTime From { get; set; }
public DateTime To { get; set; }
// valid applicable restriction range
public int Minimum { get; set; }
public int Maximum { get; set; }
}
I could then have
IList<Restricton> restrictions;
and then searching for restrictions that are applied on a particular day
restrictions.Where(r => day >= r.From && day <= r.To);
Issue
I suppose using IList<T>
isn't the best option because I will be doing a lot of searches over these restrictions and every time I would call LINQ method .Where
the whole collection would be enumerated and filtered.
From the SQL knowledge I have I know that table scan is always worse than an index scan so I would like to apply a similar logic here. Instead of enumerating the whole collection every time I would rather filter in a more intelligent way.
Question
What would be a better (faster) way to enumerate my restrictions so my algorithm wouldn't be enumerating over them every time I'd want to filter out a few?
I was thinking of IDictionary<K,V>
but it would still need to scan them all because my restrictions are not set per day, but rather per day range.
What would you suggest?