How to flatten nested objects with linq expression
Solution 1:
myBooks.SelectMany(b => b.Chapters
.SelectMany(c => c.Pages
.Select(p => b.Name + ", " + c.Name + ", " + p.Name)));
Solution 2:
Assuming books
is a List of Book:
var r = from b in books
from c in b.Chapters
from p in c.Pages
select new {BookName = b.Name, ChapterName = c.Name, PageName = p.Name};
Solution 3:
myBooks.SelectMany(b => b.Chapters
.SelectMany(c => c.Pages
.Select(p => new
{
BookName = b.Name ,
ChapterName = c.Name ,
PageName = p.Name
});