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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:37:09+00:00 2026-06-15T08:37:09+00:00

suppose I have this query : int[] Numbers= new int[5]{5,2,3,4,5}; var query = from

  • 0

suppose I have this query :

  int[] Numbers= new int[5]{5,2,3,4,5};

  var query =  from a in Numbers
      where a== Numbers.Max (n => n) //notice MAX  he should also get his value somehow
      select a;

foreach (var element in query)
  Console.WriteLine (element);
  • How many times does Numbers is enumerated when running the foreach ?

  • how can I test it ( I mean , writing a code which tells me the number of iterations)

  • 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-15T08:37:10+00:00Added an answer on June 15, 2026 at 8:37 am

    It will be iterated 6 times. Once for the Where and once per element for the Max.

    The code to demonstrate this:

    private static int count = 0;
    public static IEnumerable<int> Regurgitate(IEnumerable<int> source)
    {
        count++;
        Console.WriteLine("Iterated sequence {0} times", count);
        foreach (int i in source)
            yield return i;
    }
    
    int[] Numbers = new int[5] { 5, 2, 3, 4, 5 };
    
    IEnumerable<int> sequence = Regurgitate(Numbers);
    
    var query = from a in sequence
                where a == sequence.Max(n => n)
                select a;
    

    It will print “Iterated sequence 6 times”.

    We could make a more general purpose wrapper that is more flexible, if you’re planning to use this to experiment with other cases:

    public class EnumerableWrapper<T> : IEnumerable<T>
    {
        private IEnumerable<T> source;
        public EnumerableWrapper(IEnumerable<T> source)
        {
            this.source = source;
        }
    
        public int IterationsStarted { get; private set; }
        public int NumMoveNexts { get; private set; }
        public int IterationsFinished { get; private set; }
    
        public IEnumerator<T> GetEnumerator()
        {
            IterationsStarted++;
    
            foreach (T item in source)
            {
                NumMoveNexts++;
                yield return item;
            }
    
            IterationsFinished++;
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public override string ToString()
        {
            return string.Format(
    @"Iterations Started: {0}
    Iterations Finished: {1}
    Number of move next calls: {2}"
    , IterationsStarted, IterationsFinished, NumMoveNexts);
    
        }
    }
    

    This has several advantages over the other function:

    1. It records both the number of iterations started, the number of iterations that were completed, and the total number of times all of the sequences were incremented.
    2. You can create different instances to wrap different underlying sequences, thus allowing you to inspect multiple sequences per program, instead of just one when using a static variable.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this LINQ query (on .NET 4.0) : IEnumerable<Service> listService = (from
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have simple table like this: ID (PRIMARY) time (INT) stage (TINYINT) other
Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
Suppose I have this table parent | child 1 2 1 3 2 4
Suppose I have this table: id | name | city ------------------ 1 | n1
Suppose I have this function: void my_test() { A a1 = A_factory_func(); A a2(A_factory_func());
Suppose I have this feature branch foo. Now I want to merge it back
Suppose I have this: <select id=myselect> <option rel=a>1</option> <option rel=b>2</option> <select> How do I

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.