Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:56+00:00 2026-05-25T20:58:56+00:00

Sometimes I expect a certain range of items and need to do some validation

  • 0

Sometimes I expect a certain range of items and need to do some validation to ensure that I am within that range. The most obvious 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:58:57+00:00Added an answer on May 25, 2026 at 8:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I have a batch file like this: Action.bat : Set PATH=%PATH%;C:\Some\Folder\I\Need foo.exe Bar1
Sometimes I need to quickly extract some arbitrary data from XML files to put
I'm learning Expect, and I've noticed that my Expect scripts sometimes reach a point
Sometimes I have to work on code that moves the computer clock forward. In
Sometimes we deploy applications behind customer firewall and we need read only access to
What behavior should I expect if I delete a DOM element that was used
I have a web application that produce its reports in HTML format. Sometimes these
When processing a POST request in the Django views.py file, I sometimes need to
I need to support decoding ISO-8859-1 in my e-mail client. Specifically sometimes messages contain
At a point in my code, I expect current_part to sometimes be nil ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.