Michael Tot Korsgaard
Michael Tot Korsgaard

Reputation: 4014

foreach list<object> in object

I want to make a foreach for each item in a list. But it doesn't seem to be possible. Or am I doing it wrong?

The C# class Group

public int _id;
public string name;
public List<SubGroups> subs;

The C# class SubGroup

public int _id;
public string name;

Normally I would do something like this

// make group = all groups
foreach(var g in group)
{
    foreach(var s in group.subs)
    {
        // Do something
    }
}

But I can't call the group.subs or at least it won't show. Is this even possible?

Upvotes: 0

Views: 3422

Answers (1)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

foreach(var g in group)
{
    foreach(var s in g.subs)
    {
        // Do something
    }
}

You have to use the variable in your outer loop; which is the current variable in the iteration.

Upvotes: 8

Related Questions