1

I have the following lists of string

List<string> List1 = new List<string> { "P1", "P2", "P3" };
List<string> List2 = new List<string> { "Q1", "Q2", "Q3" };
List<string> List3 = new List<string> { "R1", "R2", "R3" };

//........
// Add List1,List2, List3 values Vertically  to CombileList

CombineList = { "P1", "Q1", "R1", "P2", "Q2", "R2", "P3", "Q3", "R3" };

I want to Add Values to CombineList from all lists vertically, as shown in CombineList, there can be n number of lists to add to CombineList the same way.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Shakir.iti
  • 103
  • 1
  • 3
  • 13

3 Answers3

0

If the lists are the same size you could use a for loop:

List<string> list1 = new List<string> { "P1", "P2", "P3" };
List<string> list2 = new List<string> { "Q1", "Q2", "Q3" };
List<string> list3 = new List<string> { "R1", "R2", "R3" };

List<string> combinedList = new List<string>();

for(int i = 0; i < list1.Count; i++)
{
    combinedList.Add(list1[i]);
    combinedList.Add(list2[i]);
    combinedList.Add(list3[i]);
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
0

Similar question asked Here

using IEnumerator and the MoveNext() method, you can loop over the arrays and combine them how you like

Community
  • 1
  • 1
0

By using Enumerators:

public List<T> CombineVertically<T>(List<List<T>> Source)
        {
            List<T> result = new List<T>();

            var enumerators = Source.Select(x => x.GetEnumerator());
            while (enumerators.Where(x => x.MoveNext()).Count() > 0)            
                result.AddRange(enumerators.Select(x => x.Current));

            enumerators.ToList()
                .ForEach(x => x.Dispose());

            return result;
        }