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

  • Home
  • SEARCH
  • 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 8250681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:58:35+00:00 2026-06-07T23:58:35+00:00

If I wrote : for (int i = 0; i < Strutture.Count(); i++) {

  • 0

If I wrote :

for (int i = 0; i < Strutture.Count(); i++)
{
}

and Strutture is an IEnumerable with 200 elements, IIS crash. That’s because I see every time I do Strutture.Count() it executes all LINQ queries linked with that IEnumerable.

So, how can I get the “current” number of elements? I need a list?

  • 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-06-07T23:58:38+00:00Added an answer on June 7, 2026 at 11:58 pm

    “That’s because I see every time I do Strutture.Count() it executes all LINQ queries linked with that IEnumerable.”

    Without doing such, how is it going to know how many elements there are?

    For example:

    Enumerable.Range(0,1000).Where(i => i % 2==0).Skip(100).Take(5).Count();
    

    Without executing the LINQ, how could you know how many elements there are?

    If you want to know how many elements there are in the source (e.g. Enumerable.Range) then I suggest you use a reference to that source and query it directly. E.g.

     var numbers = Enumerable.Range(0,1000);
     numbers.Count();
    

    Also keep in mind some data sources don’t really have a concept of ‘Count’ or if they do it involves going through every single item and counting them.

    Lastly, if you’re using .Count() repetitively [and you don’t expect the value to actually change] it can be a good idea to cache:

     var count = numbers.Count();
     for (int i =0; i<count; i++) //  Do Something
    

    Supplemental:

    “At first Count(), LINQ queries are executes. Than, for the next, it just “check” the value 🙂 Not “execute the LINQ query again…” :)” – Markzzz

    Then why don’t we do that?

    var query = Enumerable.Range(0,1000).Where(i => i % 2==0).Skip(100).Take(5).Count();
    var result = query.ToArray() //Gets and stores the result!
    result.Length;   
    

    🙂

    “But when I do the first “count”, it should store (after the LINQ queries) the new IEnumerable (the state is changed). If I do again .Count(), why LINQ need to execute again ALL queries.” – Markzzz

    Because you’re creating a query that gets compiled down into X,Y,Z. You’re running the same query twice however the result may vary.

    For example, check this out:

        static void Main(string[] args)
        {
            var dataSource = Enumerable.Range(0, 100).ToList();
            var query = dataSource.Where(i => i % 2 == 0);
    
            //Run the query once and return the count:
            Console.WriteLine(query.Count());  //50
    
            //Now lets modify the datasource - remembering this could be a table in a db etc.
            dataSource.AddRange(Enumerable.Range(100, 100));
    
            //Run the query again and return the count:
            Console.WriteLine(query.Count());  //100
            Console.ReadLine();
        }
    

    This is why I recommended storing the results of the query above!

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

Sidebar

Related Questions

Just because I've never read binary files before I wrote a program that reads
in c++ I can wrote: int someArray[8][8]; for (int i=0; i < 7; i++)
I wrote my comparison function int cmp(const int * a,const int * b) {
i wrote a simple function in controller public string LinkProjectSquareFilter(int squareId) { return squareId.ToString();
I wrote in // In file t.h #ifndef __t_h__ #define __t_h__ static int abc;
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I wrote a simple C program: #include <unistd.h> #include <stdio.h> int main( int argc,
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
The problem appears with the insert function that I wrote. 3 conditions must work,

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.