How to reduce multiple nested foreach blocks

Viewed 2088

I have the following scenario:

var Ids = object1.GetIds(); // returns IEnumerable<int>
foreach (var id in Ids)
{
    foreach (var relatedObject in object1.GetRelatedObjects(id))
    {
    // Do Something with related object
    }
}

In this case, i want to get rid of from the first foreach and reduce this logic into single foreach. How could i achieve this?

Should it be possible with LINQ expression some similar methodology?

2 Answers
Related