This is a build off of How to flatten nested objects with linq expression as I don't have enough reputation to add comments to the discussion.
I'm trying to flatten a tree structure of different types into a single list.
Here is the sample model:
public class Book
{
public string Name { get; set; }
public IList<Chapter> Chapters { get; set; }
}
public class Chapter
{
public string Name { get; set; }
public IList<Page> Pages { get; set; }
}
public class Page
{
public string Name { get; set; }
}
And sample data:
Book: Pro Linq
{
Chapter 1: Hello Linq
{
Page 1,
Page 2,
Page 3
},
Chapter 2: C# Language enhancements
{
Page 4
},
Chapter 3: Glossary
{
}
}
Book: Pro Linq II
{
}
And the desired flat output:
"Pro Linq", "Hello Linq", "Page 1"
"Pro Linq", "Hello Linq", "Page 2"
"Pro Linq", "Hello Linq", "Page 3"
"Pro Linq", "C# Language enhancements", "Page 4"
"Pro Linq", "Glossary", null
"Pro Linq II", null, null
Is there any way to do this without concat and without processing the collection twice?