开发者

Check if an IEnumerable has less than a certain number of items without causing any unnecessary evaluation?

Sometimes I expect a certain range of items and need to do some validation to ensure that I am within that range. The most obvi开发者_开发百科ous way to do this is to just compare the number of items in the collection with the range.

public static bool IsWithinRange<T>(this IEnumerable<T> enumerable, int max)
{
    return enumerable.Count() <= max;
}

Although, my understanding is that the linq Count() method will evaluate the entire enumerable before returning a result. Ideally I would only cause evaluation on the minimal number of items to get my result.

What would be the best way to ensure that an enumerable has less than a certain number of items without causing any unnecessary evaluation?


Don't use Count(), as you already know, the entire collection would have to be traversed in general.

You can do this instead:

public static bool IsWithinRange<T>(this IEnumerable<T> enumerable, int max)
{
    return !enumerable.Skip(max).Any();
}

Note you will still have to enumerate over the first max items in the collection, that's unavoidable unless you try to make some assumptions about the underlying collection.


To really optimize this further, you can check if the underlying type is an ICollection<> or ICollection in order to access the Count property. That way you don't have to enumerate over the items at all. Otherwise fallback on enumerating the items.

public static bool IsWithinRange<T>(this IEnumerable<T> enumerable, int max)
{
    var asCollection = enumerable as System.Collections.ICollection;
    if (asCollection != null) return asCollection.Count <= max;
    var asGenericCollection = enumerable as ICollection<T>;
    if (asGenericCollection != null) return asGenericCollection.Count <= max;
    return !enumerable.Skip(max).Any();
}

Of course this isn't exactly free as you're doing additional checks, but it beats having to enumerate over the collection if at all possible, particularly if max is large.


There are several different ways to do this. Perhaps the simplest is:

public static bool IsWithinRange<T>(this IEnumerable<T> enumerable, int max)
{
    return enumerable.Take(max+1).Count() <= max;
}


You could combine Count() with Take().

return enumerable.Take(max + 1).Count() <= max;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜