148

I am trying to flatten nested objects like this:

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; }
}

Let me make an example. This is the data I have

Book: Pro Linq 
{ 
   Chapter 1: Hello Linq 
   {
      Page 1, 
      Page 2, 
      Page 3
   },
   Chapter 2: C# Language enhancements
   {
      Page 4
   },
}

The result I am looking for is the following flat list:

"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"

How could I accomplish this? I could do it with a select new but I've been told that a SelectMany would be enough.

bkwdesign
  • 1,953
  • 2
  • 28
  • 50
ab_732
  • 3,639
  • 6
  • 45
  • 61

4 Answers4

239
myBooks.SelectMany(b => b.Chapters
    .SelectMany(c => c.Pages
        .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ? – ab_732 Jun 21 '11 at 17:02
  • 3
    @abx78: simply alter the last select: `.Select(p => new FlatBook(b.Name, c.Name, p.Name))` – user7116 Jun 21 '11 at 17:22
  • 1
    does this produce the same result? `myBooks.SelectMany(b => b.Chapters).SelectMany(c => c.Pages).Select(p => b.Name + ", " + c.Name + ", " + p.Name);` – Homer Mar 27 '13 at 16:46
  • So how would you rewrite this to support an N number of nested objects? – The Muffin Man Aug 14 '13 at 22:08
  • Is there a way to do this but include any Books that do not have Chapters? – Mastro Jun 24 '14 at 20:29
  • @Mastro start it with `myBooks.Where(b => b.Chapters != null)` – Yuriy Faktorovich Jun 24 '14 at 20:35
  • Hmm well I wanted a list where both are shown. what you have here is where not null, I wanted a flatten list where I also, include the nulls but doesn't seem to work if I do Where(b => b.Chapters == null) as I get 0. Would like to have a flatten list with all books, ones with or without chapters. Is that possible? Or do I need some type of union? – Mastro Jun 24 '14 at 20:41
  • 1
    @Mastro how about `myBooks.SelectMany(b => b.Chapters == null || !b.Chapters.Any()? new []{b.Name + " has no Chapters"} : b.SelectMany(c => c.Pages.Select(p => b.Name + ", " + c.Name + ", " + p.Name)));` – Yuriy Faktorovich Jun 24 '14 at 20:48
  • Ok that code didn't help but it got my on my track to solving it. So gonna vote up your comment for the effort. – Mastro Jun 24 '14 at 21:58
  • @Mastro did I misunderstand what you were asking? What was the solution? – Yuriy Faktorovich Jun 24 '14 at 23:56
  • Well I couldn't get yours to compile but I don't have books to chapters, I have projects(b) to companyperson(c) to companies(p). So I used linqPad and created a left join query. Too long to post here it seems, I'll post as an answer. – Mastro Jun 25 '14 at 00:47
  • @Homer: I don't think your solution is possible, since `b` and `c` are inaccessible from the last `Select` clause. See live demo: http://rextester.com/BHNH3184 – Dejavu Mar 30 '17 at 19:22
  • @Dejavu, yeah my comment stinks. I'm not even sure what the point of it was. – Homer Apr 04 '17 at 15:26
  • @YuriyFaktorovich but book.name is missing. this code only joins data from chapters and its childs – mko Mar 16 '18 at 14:44
  • Succinct! Thanks. – Pouya BCD Oct 10 '18 at 14:53
57

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};
shA.t
  • 16,580
  • 5
  • 54
  • 111
boca
  • 2,352
  • 19
  • 21
7
myBooks.SelectMany(b => b.Chapters
    .SelectMany(c => c.Pages
        .Select(p => new 
                {
                    BookName = b.Name ,
                    ChapterName = c.Name , 
                    PageName = p.Name
                });
  • 8
    While this code sample may answer the question, it lacks explanation. As it stands now, it adds no value, and stands the change of being downvoted / deleted. Please add some explanation what is does and why it is a solution for the problem of the OP. – oɔɯǝɹ Feb 02 '15 at 10:07
1

I was trying to do this as well, and from Yuriy's comments and messing with linqPad I have this..

Note that I don't have books, chapters, pages, I have person (books), companyPerson (chapters) and companies (pages)

from person in Person
                           join companyPerson in CompanyPerson on person.Id equals companyPerson.PersonId into companyPersonGroups
                           from companyPerson in companyPersonGroups.DefaultIfEmpty()
                           select new
                           {
                               ContactPerson = person,
                               ContactCompany = companyPerson.Company
                           };

or

Person
   .GroupJoin (
      CompanyPerson, 
      person => person.Id, 
      companyPerson => companyPerson.PersonId, 
      (person, companyPersonGroups) => 
         new  
         {
            person = person, 
            companyPersonGroups = companyPersonGroups
         }
   )
   .SelectMany (
      temp0 => temp0.companyPersonGroups.DefaultIfEmpty (), 
      (temp0, companyPerson) => 
         new  
         {
            ContactPerson = temp0.person, 
            ContactCompany = companyPerson.Company
         }
   )

Ref site I used: http://odetocode.com/blogs/scott/archive/2008/03/25/inner-outer-lets-all-join-together-with-linq.aspx

Mastro
  • 1,477
  • 2
  • 22
  • 52