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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:37:28+00:00 2026-05-27T11:37:28+00:00

I know this question has been asked many times before but I tried out

  • 0

I know this question has been asked many times before but I tried out the answers and they don’t seem to work.

I have two lists of the same length but not the same type, and I want to iterate through both of them at the same time as list1[i] is connected to list2[i].

Eg:

Assuming that i have list1 (as List<string>) and list2 (as List<int>)

I want to do something like

foreach( var listitem1, listitem2 in list1, list2)
{
   // do stuff
}

Is this possible?

  • 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-27T11:37:29+00:00Added an answer on May 27, 2026 at 11:37 am

    Edit – Iterating whilst positioning at the same index in both collections

    If the requirement is to move through both collections in a ‘synchronized’ fashion, i.e. to use the 1st element of the first collection with the 1st element of the second collection, then 2nd with 2nd, and so on, without needing to perform any side effecting code, then see @sll’s answer and use .Zip() to project out pairs of elements at the same index, until one of the collections runs out of elements.

    More Generally

    Instead of the foreach, you can access the IEnumerator from the IEnumerable of both collections using the GetEnumerator() method and then call MoveNext() on the collection when you need to move on to the next element in that collection. This technique is common when processing two or more ordered streams, without needing to materialize the streams.

    var stream1Enumerator = stream1.GetEnumerator();
    var stream2Enumerator = stream2.GetEnumerator();
    var currentGroupId = -1; // Initial value
    // i.e. Until stream1Enumerator runs out of 
    while (stream1Enumerator.MoveNext())
    {
       // Now you can iterate the collections independently
       if (stream1Enumerator.Current.Id != currentGroupId)
       {
           stream2Enumerator.MoveNext();
           currentGroupId = stream2Enumerator.Current.Id;
       }
       // Do something with stream1Enumerator.Current and stream2Enumerator.Current
    }
    

    As others have pointed out, if the collections are materialized and support indexing, such as an ICollection interface, you can also use the subscript [] operator, although this feels rather clumsy nowadays:

    var smallestUpperBound = Math.Min(collection1.Count, collection2.Count);
    for (var index = 0; index < smallestUpperBound; index++)
    {
         // Do something with collection1[index] and collection2[index]
    }
    

    Finally, there is also an overload of Linq’s .Select() which provides the index ordinal of the element returned, which could also be useful.

    e.g. the below will pair up all elements of collection1 alternatively with the first two elements of collection2:

    var alternatePairs = collection1.Select(
        (item1, index1) => new 
        {
            Item1 = item1,
            Item2 = collection2[index1 % 2]
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before, but many answers don't give clear
I know this question has been asked before numorous times but they seem to
I know this is a question that has been asked many times before, but
I know this question has been asked many times before but I can't find
I know this question has been asked so many times before but I couldn't
I know this question has been asked many times before, but any of the
I know this question has been asked many times before, but I haven't found
Ok I know this question has been asked many many many times before, but
I know this question has been asked before, but none of the previous answers
I know this question has been asked here before, but I don't think those

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.