如何使用linq表达式展平嵌套对象


125

我正在尝试展平这样的嵌套对象:

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

让我举个例子。这是我的数据

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

我正在寻找的结果是以下平面列表:

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

我该怎么做?我可以使用select new来做到这一点,但有人告诉我SelectMany就足够了。

Answers:


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

太棒了!!! 如果我得到一个新对象,例如FlatBook {BookName,ChapterName,PageName},该怎么办?
2011年

2
@ abx78:只需更改最后一个选择:.Select(p => new FlatBook(b.Name, c.Name, p.Name))
user7116 2011年

谢谢你们,这就是我所需要的!
2011年

1
这会产生相同的结果吗?myBooks.SelectMany(b => b.Chapters).SelectMany(c => c.Pages).Select(p => b.Name + ", " + c.Name + ", " + p.Name);
荷马

1
@Mastro怎么样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

50

假设books有一个书单:

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

2
+1(即使IEnumerable<Book>可以做到)也不需要List<Book>
user7116 2011年

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

7
尽管此代码示例可以回答问题,但缺乏解释。就目前而言,它没有任何价值,并且可以承受被低估/删除的变化。请添加一些解释,这是什么以及为什么它可以解决OP问题。
oɔɯǝɹ

0

我也尝试这样做,从尤里(Yuriy)的评论和linqPad的混乱中我有这个。

请注意,我没有书籍,章节,页面,我没有人(书),companyPerson(章)和公司(页面)

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

要么

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

我使用的参考网站:http : //odetocode.com/blogs/scott/archive/2008/03/25/inner-outer-lets-all-join-together-with-linq.aspx

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.