-2

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.

edit : im trying to get each list result in one foreach.

sorry im bad at english

  • 1
    It is very unclear what you want. do you want a single list of strings with all the elements of all the other lists or you want to loop for all first elements in all list then second.... and so on ? – Franck Mar 10 '22 at 11:54
  • A `List` can hold anything. In particular, it could hold a `List`, so you can have a `List>`. – Jeroen Mostert Mar 10 '22 at 11:54
  • sorry im bad at english, what im trying to do is to get each list result in single foreach – terdexx pogi Mar 10 '22 at 12:07
  • https://stackoverflow.com/questions/18395943/using-foreach-to-iterate-simultaneously-through-multiple-lists-syntax-sugar i tried the answered code from here and it worked but it is limited for only 7 list after 7th , 8th to 15th list it didnt have a result – terdexx pogi Mar 10 '22 at 12:10
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 10 '22 at 12:14
  • What are these different lists for? Are you sure you don't need a class with 15 properties? – Daniel Mann Mar 10 '22 at 21:15

2 Answers2

0

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.

JonasH
  • 28,608
  • 2
  • 10
  • 23
-1

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];
}
shingo
  • 18,436
  • 5
  • 23
  • 42