In Listing 13-4, the author provides the following logic for obtaining the most prolific author (i.e. the author with the greatest number of books) in the example given:
Code:
var prolificAuthor = (
from author in authors
orderby author.Books.Count() descending
select author
).First();
While this works, it is not as efficient as it could be, in that it relies on ordering the query, which is a very expensive operation.
An alternative way of doing this would be as follows:
Code:
var prolificAuthor =
(from author in authors
where author.Books.Count() == authors.Max(auth => auth.Books.Count())
select author).First();
This code selects the first author from a list of authors in which their book count equals the maximum book count. It accomplishes the same thing, but is much quicker, as it does not involve any ordering.