Visual C# Developer Center > Visual C# Forums > Visual C# General > Multiple ForEachs, Possible To Avoid With LINQ?
Ask a questionAsk a question
 

AnswerMultiple ForEachs, Possible To Avoid With LINQ?

  • Friday, November 06, 2009 3:38 PMphpnuke23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I am wondering about getting rid of some nested foreach's in my code using LINQ. For example, assume the following code:

                      <pre>  IEnumerable<MyObject1> MyObject1Collection = SomeCollection.Cast<MyObject1>()
                        foreach (MyObject1 obj1 in MyObject1Collection)
                        {
                            foreach (MyObject2 obj2 in MyObject1.MyObject2Collection)                           {                           
    
    if (MyObject2.IsFalse)
    {
    // Do Stuff
    }
    
    }
                        }
    
    Is there a better way to do this with LINQ?

    TIA!

Answers

All Replies