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 6674625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:46:03+00:00 2026-05-26T03:46:03+00:00

Based on the following code: var grouped = filters.GroupBy(p => p.PropertyName); int numOfRowElements =

  • 0

Based on the following code:

 var grouped = filters.GroupBy(p => p.PropertyName);
                 int numOfRowElements = grouped.Count();
     foreach (IGrouping<string, PropertyFilter> filter in grouped)
                {


                    foreach (var propertyFilter in filter)
                    {
                        // do something
                    }

                }

where filters a List, my understanding is that calling IEnumerable.Count() forces the query to be executed. Is the result of this execution stored in the grouped variable, which is then used in the foreach loop, or does the foreach loop force the query to be executed again? Would it be better to do this instead?

 var grouped = filters.GroupBy(p => p.PropertyName).ToList();
  int numOfRowElements = grouped.Count;
     foreach (IGrouping<string, PropertyFilter> filter in grouped)
                {


                    foreach (var propertyFilter in filter)
                    {
                        // do something
                    }

                }

TIA.

  • 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-26T03:46:04+00:00Added an answer on May 26, 2026 at 3:46 am

    If the underlying datasource is IList<T>, Enumerable.Count() will invoke the .Count property as an optimization, so there is no* performance penalty. If it is not, an enumeration will be forced. Consider this carefully.

    var someList = new List<int>(); 
    var count = someList.Count(); // will use .Count property
    var count = someList.OrderBy(x => x).Count(); // will force enumeration 
    

    In this example, I’m just getting the count of the list in the second statement. In the third, I’m ordering the list and then getting the count. Ordering the list returns a sequence, not a list. Therefore, the Count() method is not working on an IList<T>, but an IEnumerable<T>. In this case, the query must be enumerated to acquire the result and will incur whatever cost that comes along with it (in this case, the ordering).

    In light of this, in your first snippet, you will enumerate your query twice. Once to get the count, once in the foreach. This will perform all the logic to group your data twice. Your second example will perform the grouping operations just once, while obviously iterating over the resulting list in the foreach, which should be less expensive than also performing the grouping operation a second time. (Whether you can measure a savings will entirely depend upon the size and/or source of the data in the original list. When in doubt, profile it.)


    *There may be a small measured penalty for the layer of indirection, you’ll have to profile this if you believe it is a true bottleneck. But think of the Count() method as

    if (sequence is IList<T>) 
    {
        return ((IList<T>)sequence).Count
    }
    else 
    {
       /* perform enumeration */;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public IDictionary<string, int> GetCountByDate(DateTime fromDate, DateTime toDate) { var
Take the following EF based code: var newItem = context.Items.CreateObject(); newItem.ID = Guid.NewGuid(); newItem.Name
I am currently running the following code based on Chapter 12.5 of the Python
My question is concerning SQL connection status, load, etc. based on the following code:
In the following code, the stack-based variable 'ex' is thrown and caught in a
I have the following code in WCF service to throw a custom fault based
Gday All, The following code displays a div based on the option that has
I have the following HTML code based on a tab menu format, i.e: <div
I'm not a javascript guru. I've got the following code below: var aCookieValues =
Based on the following code... A user can come along and add as many

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.