-3

Is there a way to make multiple data field merge together and become a single List. There is two array which is

int[] QueueNo = {10, 20, 30, 40};
int [] WaitingTime = {1, 2, 3, 4}

How do I insert this two data into one List So that I can a foreach loop to insert the data into the database

foreach(var item in list)
{
    ////Code For Create Data
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • `foreach(var item in QueueNo.Concat(WaitingTime))` -> no need to create a list; just enumerate both arrays. – Johnathan Barclay Mar 17 '21 at 09:54
  • Duplicates: https://stackoverflow.com/questions/1528171/joining-two-lists-together and https://stackoverflow.com/questions/59217/merging-two-arrays-in-net – Xerillio Mar 17 '21 at 10:00
  • I doubt OP wants to **append** one list to the other, but create one containing the analogous elements from both lists. – MakePeaceGreatAgain Mar 17 '21 at 10:01
  • @JohnathanBarclay If let say I had 2 more field which is ```Name``` and ```PhoneNo``` I will just need to write like this ```foreach (var item in QueueNo.Concat(WaitingTime).Concat(Name).Concat(PhoneNo))``` and so on if I had more field. – NewbieCoder Mar 17 '21 at 10:02
  • @Xerillio please read carefully is not a duplicate. – NewbieCoder Mar 17 '21 at 10:06
  • So is the question, given `x` number of arrays of the same length `y`, how do you create an array of values containing `x` fields of `y` length? – Johnathan Barclay Mar 17 '21 at 10:09
  • @NewbieCoder Perhaps you can elaborate and explain why those are not duplicates? The question is not clear about that. Is it as Johnathan explains and as HimBromBeere answers? – Xerillio Mar 17 '21 at 10:12
  • what is the expected output? Is it combining two lists or a key-value pair of QueueNo and Waiting Time? – Akshay G Mar 17 '21 at 11:26

2 Answers2

0

You can zip both lists together:

var result = QueueNo.Zip(WaitingTime, (first, second) => new { first, second });

Now you can foreach this list:

foreach(var e in result)
{
    var queueNo = e.first;
    var waitingTime = e.second;
}

Alternativly use a normal for-loop on one of the arrays and use the common index:

for(int i = 0; i < QueueNo.Length; i++)
{
    var queNo = QueueNo[i];
    var waitingTime = WaitingTime[i];
}
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

You can easily do it. Just use Union

int[] QueueNo = {10, 20, 30, 40};
int [] WaitingTime = {1, 2, 3, 4}
var myList = QueueNo.Union(WaitingTime).ToList();
Mehran Gharzi
  • 136
  • 1
  • 10