Home > Blockchain >  c# trying to achieve more than ten list in single foreach
c# trying to achieve more than ten list in single foreach

Time:03-10

i have more than 10 list and i want to put it inside single foreach i tried (Tuple.Create) but it is limited for only 7 items and also tried (from in) the result of first item from it is repeated.

List<string> ALLmlist1 = new List<string>();
List<string> ALLmlist2 = new List<string>();
List<string> ALLmlist3 = new List<string>();
List<string> ALLmlist4 = new List<string>();
List<string> ALLmlist5 = new List<string>();
List<string> ALLmlist6 = new List<string>();
List<string> ALLmlist7 = new List<string>();
List<string> ALLmlist8 = new List<string>();
List<string> ALLmlist9 = new List<string>();
List<string> ALLmlist10 = new List<string>();
List<string> ALLmlist11 = new List<string>();
List<string> ALLmlist12 = new List<string>();
List<string> ALLmlist13 = new List<string>();
List<string> ALLmlist14 = new List<string>();
List<string> ALLmlist15 = new List<string>();
foreach(var item in all15list)
{
var i1 = item.Item1;
var i2 = item.Item2;
{

trying to look like this, hope you guys have other option thanks.

CodePudding user response:

You should probably use a nested list. I.e.

var outerList = new List<List<string>>();
for(var outerIndex = 0; outerIndex < outerList.Count; outerIndex  ){
    var innerList = outerList[outerIndex];
    for(var innerIndex = 0; innerIndex < innerList.Count; innerIndex  ){
        var value = innerList[innerIndex];
        //Do something with each value
    }       
}

If you have a bunch of separate lists and want to create a nested list you can use a collection initializer:

var outerList = new List<List<string>>(){
    ALLmlist1 ,
    ALLmlist2,
    ...
    } ;

That said, keeping a bunch of list in that way is somewhat of a design smell. Usually it would represent something, like objects, 2D data, or something else. And if so it might be better to use some kind of specialized type that better represent the data.

CodePudding user response:

Is this what you want?

var minCount = new List<int>
{
    ALLmlist1.Count,
    ALLmlist2.Count,
    ...
    ALLmlist15.Count,
}.Min();

for (var i = 0; i < minCount; i  )
{
    var i1 = ALLmlist1[i];
    var i2 = ALLmlist2[i];
    ...
    var i15 = ALLmlist15[i];
}
  • Related