Assuming the types you describe look something like this:
class Library
{
public HashSet<Book> Books { get; }
}
class Book
{
public List<Page> Pages { get; }
}
There are several ways to write such a request.
Library lib = ...;
var count1 = lib.Books.Sum(b => b.Pages.Count);
var count2 = lib.Books.Select(b => b.Pages.Count).Sum();
var count3 = lib.Books.Aggregate((sum, book) => sum + book.Pages.Count);
Of course, there are many ways you can articulate this. Personally, I would write it using the first method.
source
share