How to flatten nested objects with linq expression












103















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.










share|improve this question





























    103















    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.










    share|improve this question



























      103












      103








      103


      23






      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.










      share|improve this question
















      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.







      c# .net vb.net linq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 9 '14 at 17:21









      bkwdesign

      765935




      765935










      asked Jun 21 '11 at 16:44









      abx78abx78

      1,55732644




      1,55732644
























          4 Answers
          4






          active

          oldest

          votes


















          168














          myBooks.SelectMany(b => b.Chapters
          .SelectMany(c => c.Pages
          .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));





          share|improve this answer
























          • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

            – abx78
            Jun 21 '11 at 17:02








          • 2





            @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

            – user7116
            Jun 21 '11 at 17:22











          • Thank you guys, this was what I need!

            – abx78
            Jun 21 '11 at 18:06











          • 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






          • 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





















          46














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





          share|improve this answer





















          • 1





            +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

            – user7116
            Jun 21 '11 at 17:02











          • Thas a good point. Thanks

            – boca
            Jun 21 '11 at 17:12






          • 1





            +1 for the alternative version

            – tobsen
            Jan 19 '13 at 22:09



















          1














          myBooks.SelectMany(b => b.Chapters
          .SelectMany(c => c.Pages
          .Select(p => new
          {
          BookName = b.Name ,
          ChapterName = c.Name ,
          PageName = p.Name
          });





          share|improve this answer



















          • 5





            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 2 '15 at 10:07



















          0














          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






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6428940%2fhow-to-flatten-nested-objects-with-linq-expression%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            168














            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));





            share|improve this answer
























            • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

              – abx78
              Jun 21 '11 at 17:02








            • 2





              @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

              – user7116
              Jun 21 '11 at 17:22











            • Thank you guys, this was what I need!

              – abx78
              Jun 21 '11 at 18:06











            • 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






            • 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


















            168














            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));





            share|improve this answer
























            • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

              – abx78
              Jun 21 '11 at 17:02








            • 2





              @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

              – user7116
              Jun 21 '11 at 17:22











            • Thank you guys, this was what I need!

              – abx78
              Jun 21 '11 at 18:06











            • 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






            • 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
















            168












            168








            168







            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));





            share|improve this answer













            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => b.Name + ", " + c.Name + ", " + p.Name)));






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 21 '11 at 16:49









            Yuriy FaktorovichYuriy Faktorovich

            54.2k1190126




            54.2k1190126













            • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

              – abx78
              Jun 21 '11 at 17:02








            • 2





              @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

              – user7116
              Jun 21 '11 at 17:22











            • Thank you guys, this was what I need!

              – abx78
              Jun 21 '11 at 18:06











            • 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






            • 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





















            • Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

              – abx78
              Jun 21 '11 at 17:02








            • 2





              @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

              – user7116
              Jun 21 '11 at 17:22











            • Thank you guys, this was what I need!

              – abx78
              Jun 21 '11 at 18:06











            • 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






            • 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



















            Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

            – abx78
            Jun 21 '11 at 17:02







            Awesome!!! What if I would have a result a new object, like FlatBook{BookName, ChapterName, PageName} ?

            – abx78
            Jun 21 '11 at 17:02






            2




            2





            @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

            – user7116
            Jun 21 '11 at 17:22





            @abx78: simply alter the last select: .Select(p => new FlatBook(b.Name, c.Name, p.Name))

            – user7116
            Jun 21 '11 at 17:22













            Thank you guys, this was what I need!

            – abx78
            Jun 21 '11 at 18:06





            Thank you guys, this was what I need!

            – abx78
            Jun 21 '11 at 18:06













            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





            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




            1




            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







            @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















            46














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





            share|improve this answer





















            • 1





              +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

              – user7116
              Jun 21 '11 at 17:02











            • Thas a good point. Thanks

              – boca
              Jun 21 '11 at 17:12






            • 1





              +1 for the alternative version

              – tobsen
              Jan 19 '13 at 22:09
















            46














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





            share|improve this answer





















            • 1





              +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

              – user7116
              Jun 21 '11 at 17:02











            • Thas a good point. Thanks

              – boca
              Jun 21 '11 at 17:12






            • 1





              +1 for the alternative version

              – tobsen
              Jan 19 '13 at 22:09














            46












            46








            46







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 23 '16 at 13:43









            shA.t

            13.1k43871




            13.1k43871










            answered Jun 21 '11 at 16:58









            bocaboca

            1,9421518




            1,9421518








            • 1





              +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

              – user7116
              Jun 21 '11 at 17:02











            • Thas a good point. Thanks

              – boca
              Jun 21 '11 at 17:12






            • 1





              +1 for the alternative version

              – tobsen
              Jan 19 '13 at 22:09














            • 1





              +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

              – user7116
              Jun 21 '11 at 17:02











            • Thas a good point. Thanks

              – boca
              Jun 21 '11 at 17:12






            • 1





              +1 for the alternative version

              – tobsen
              Jan 19 '13 at 22:09








            1




            1





            +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

            – user7116
            Jun 21 '11 at 17:02





            +1, even though any IEnumerable<Book> will do, doesn't need a List<Book>.

            – user7116
            Jun 21 '11 at 17:02













            Thas a good point. Thanks

            – boca
            Jun 21 '11 at 17:12





            Thas a good point. Thanks

            – boca
            Jun 21 '11 at 17:12




            1




            1





            +1 for the alternative version

            – tobsen
            Jan 19 '13 at 22:09





            +1 for the alternative version

            – tobsen
            Jan 19 '13 at 22:09











            1














            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => new
            {
            BookName = b.Name ,
            ChapterName = c.Name ,
            PageName = p.Name
            });





            share|improve this answer



















            • 5





              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 2 '15 at 10:07
















            1














            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => new
            {
            BookName = b.Name ,
            ChapterName = c.Name ,
            PageName = p.Name
            });





            share|improve this answer



















            • 5





              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 2 '15 at 10:07














            1












            1








            1







            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => new
            {
            BookName = b.Name ,
            ChapterName = c.Name ,
            PageName = p.Name
            });





            share|improve this answer













            myBooks.SelectMany(b => b.Chapters
            .SelectMany(c => c.Pages
            .Select(p => new
            {
            BookName = b.Name ,
            ChapterName = c.Name ,
            PageName = p.Name
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 2 '15 at 9:41









            Madhukar BhandariMadhukar Bhandari

            197




            197








            • 5





              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 2 '15 at 10:07














            • 5





              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 2 '15 at 10:07








            5




            5





            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 2 '15 at 10:07





            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 2 '15 at 10:07











            0














            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






            share|improve this answer




























              0














              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






              share|improve this answer


























                0












                0








                0







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 25 '14 at 0:54









                MastroMastro

                94121541




                94121541






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6428940%2fhow-to-flatten-nested-objects-with-linq-expression%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini