linq question: querying nested collections

To find an answer.

questions.SelectMany(q => q.Answers).Where(a => a.Name == "SomeName")

To find the question of an answer.

questions.Where(q => q.Answers.Any(a => a.Name == "SomeName"))

In fact you will get collections of answers or questions and you will have to use First(), FirstOrDefault(), Single(), or SingleOrDefault() depending on your needs to get one specific answer or question.

Leave a Comment