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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:43:11+00:00 2026-06-13T11:43:11+00:00

I am trying to yield iterate through a collection and if the collection is

  • 0

I am trying to yield iterate through a collection and if the collection is empty then call an increment method that will get the next set of results. When the increment says there are no more results then the yield with break;

I can not use (i think) a standard IEnumerator with MoveNext() etc as the increment method returns two different types of data.

I have tried an example below but it stops after one itteration. I am hoping there is a much easier way to do this (or at least is possible just I have a bug).

static void Main(string[] args)
{
    var query = new Query();

    foreach(var s in query.Q1())
    {
        Console.WriteLine(s);
    }

    foreach (var s in query.Q2())
    {
        Console.WriteLine(s);
    }

    Console.ReadLine();
}

public class Query
{
    int i = 0;
    bool complete;
    List<string> q1 = new List<string>();
    List<string> q2 = new List<string>();

    public IEnumerable<string> Q1()
    {
        if (complete)
        {
            yield break;
        }
        if (!q1.Any() && !complete)
        {
            Increment();
        }
        if (q1.Any())
        {
            foreach (var s in q1)
            {
                yield return s;
            }
        }
    }

    public IEnumerable<string> Q2()
    {
        if (complete)
        {
            yield break;
        }
        if (!q2.Any() && !complete)
        {
            Increment();
        }
        if (q2.Any())
        {
            foreach (var s in q2)
            {
                yield return s;
            }
        }
    }

    void Increment()
    {
        if (i < 10)
        {
                    // simulate getting two types of data back (parent and two children) from datasource
            q1.Add((1 * (i + 1)).ToString());
            q2.Add("A: " + (1 * (i + 1)).ToString());
            q2.Add("B: " + (1 * (i + 1)).ToString());

            i++;
        }
        else
        {
            complete = true;
        }
    }
}

result:

1
A: 1
B: 1

Any ideas on a better way of doing this or where I am going wrong?

EDIT

Here is my rough and ready fix:

public IEnumerable<string> Q1()
{
    var index = 0;

    if (!complete)
    {
        while (!complete)
        {
            var count = q1.Count();

            if (index + 1 == count)
            {
                for (var x = index; index < count; index++)
                {
                    yield return q1[index];
                }
            }
            else
            {
                Increment();
            }
        }
    }
    else
    {
        foreach (var s in q1)
        {
            yield return s;
        }
    }
}
  • 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-13T11:43:11+00:00Added an answer on June 13, 2026 at 11:43 am

    You are adding elements only to q2 list. Thus when you call Q1 iterator, you are exiting it after checking

    if (q1.Any())
    

    When you calling Q2 iterator, you exit it after

    if (q2.Any())
    {
         foreach (var s in q2)
         {
             yield return s;
         }
    }
    

    This foreach loop is executed only once and it returns only three items which where added to q2 during single Increment call in Q1 iterator.

    It’s not very clear what you want to achieve, but here is the way you can use loop for generating return values of iterator

    public IEnumerable<string> Q2()
    {
        for (int i = 1; i <= 10; i++) // start from 1
        {
            yield return i.ToString(); // do not multiply by 1
            yield return "A: " + i;    // .ToString() is not necessary 
            yield return "B: " + i;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a counter that looks through a text and returns
I trying to write an application that has a collection of students using different
I'm trying to iterate through an array, @chem_species = [H2, S, O4] and multiply
When I'm trying get method from remote webservice it gives me error. My code
I'm trying to step through a list of records that are retrieved with find_each.
I'm trying to write a regular expression that will match a variable length string,
I'm calling a worker method that calls to the database that then iterates and
I'm trying to write an XSL that will output a certain subset of fields
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the

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.