繁体   English   中英

Linq 到对象,列表<items> ,其中项目由列表组成<items2></items2></items>

[英]Linq To Objects, List<Items>, where Items consist of List<Items2>

我有List<Book> ,在哪里

public class Book
{
    public string Name {get; set;}
    public List<Author> Authors {get; set;}
}

public class Author
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

我需要获得<FirstName, BooksCount> ,每个作者的书数。 我了解,我需要使用 GroupBy,但获取List<Author>而不是<FirstName, BooksCount>

试试这个。 我按 FullName 而不是 FirstName 进行分组,因为我非常怀疑 FirstName 可能是唯一标识符。 FullName 更好,但仍然不能是唯一的。 如果需要,您可以将.GroupBy(a => $"{a.FirstName} {a.LastName}")更改为.GroupBy(a => a.FirstName")

            List<Book> books = new List<Book>() { 
            new Book { Name = "b1", 
                Authors = new List<Author>() { 
                    new Author { FirstName = "a1fn", LastName = "a1ln" }, 
                    new Author { FirstName = "a2fn", LastName = "a2ln" } } 
            }, 
            new Book { Name = "b2", 
                Authors = new List<Author>() { 
                    new Author { FirstName = "a1fn", LastName = "a1ln" } } 
            }, new Book { Name = "b3", 
                Authors = new List<Author>() { 
                    new Author { FirstName = "a2fn", LastName = "a2ln" } } 
            }, new Book { Name = "b4",
                Authors = new List<Author>() {
                    new Author { FirstName = "a2fn", LastName = "a2ln" } }
            } };

            var result = books.SelectMany(b => b.Authors)
                .GroupBy(a => $"{a.FirstName} {a.LastName}")
                .Select(g => new { FullName = g.Key, BooksCount = g.Count() });

显然有些书是由几位作者写的。

您有一系列由一个或多个作者撰写的书籍,并且您想要一种方法将此序列转换为包含所写书籍的作者序列。

Book     Author(s)
  A      author1
  B      author2
  C      author3
  D      author1, author3
  E      author2, author3

我们首先将其展平:获取组合 [Book, Author],然后按同一作者分组:

展平后:

 Book    Author(s)
  A      author1
  B      author2
  C      author3
  D      author1,
  D      author3
  E      author2,
  E      author3

同一作者分组后:

Author      Books
author1     A, D
author2     B, E
author3     C, D, E

使用 SelectMany 进行展平 使用 GroupBy 完成每个组成员具有共同值的项目组

IEnumerable<Book> books = ...
var authorsWithTheirBooks = books.SelectMany(book => book.Authors,

    // parameter ResultSelector: take every [book, author] combination to make one new
    (book, author) => new
    {
        Book = book,
        Author = author,
    })

    // now make groups with same Author:
    .GroupBy(flattenResult => flattenResult.Author,

    // resultSelector: take every author, and the books of this author to make one new
    (author, booksOfThisAuthor => new
    {
        Author = author,
        Books = booksOfThisAuthor.ToList(),
    });

好吧,你不想要这个作者的所有书,你只想要书的数量:

    (author, booksOfThisAuthor => new
    {
        Author = author,
        Books = booksOfThisAuthor.Count(),
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM