I'm new to using lambda notation so please correct my understanding if I'm wrong. Is it possible to rewrite the a for each loop in one statement using lambda? I having the following:
Model
public partial class Section
{
public int SectionID { get; set; }
public List<Question> questions { get; set; }
//...
}
public partial class Question
{
public int QuestionID { get; set; }
public int SectionID { get; set; }
//...
}
Foreach
List<Section> sections = db.getSections.ToList();
List<Question> questions = db.getQuestions.ToList();
foreach (var section in sections)
{
List<Question> questionsInSection = new List<Question>();
foreach (var question in questions)
{
if(section.SectionID == question.SectionID)
questionsInSection.Add(question);
}
section.Questions = questionsInSection;
}