You are currently viewing the BOOK: Professional Refactoring in C# & ASP.NET section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
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.