user1134704
user1134704

Reputation: 45

substitute nested foreachs with lambdas c#

would it be possible to express the following block of code with lambdas instead of foreachs?

    IEnumerable<BODSurveys.SurveysAnwer> resp = new List<SurveysAnwer>();
    foreach (var section in Sections)
    {
      foreach (var question in section.Questions)
      {
        foreach (var answer in question.SurveysAnwers)
        {
          yield return answer;
        }
      }
    }

Upvotes: 1

Views: 113

Answers (1)

jason
jason

Reputation: 241583

Yes:

return Sections.SelectMany(s => s.Questions.SelectMany(q => q.SurveyAnswers));

Upvotes: 2

Related Questions