Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select one parent property and all children using linq

Tags:

c#

linq

I have a collection of sections and each section has a collection of questions. If I want to select all the questions under all the sections, this works

Sections.SelectMany(s=>s.Questions)

But now I also want the section number. So if I try something like this

 Sections.SelectMany(s=>s.Questions,s.SectionNumber)

it throws compilation error.

How do I make this work?

like image 216
developer747 Avatar asked Apr 23 '13 18:04

developer747


1 Answers

You should use anonymous type here:

Sections.SelectMany(s => s.Questions, (s, q) => new { Question = q, s.SectionNumber })
like image 94
MarcinJuraszek Avatar answered Oct 02 '22 17:10

MarcinJuraszek