Wrox Programmer Forums
|
BOOK: Professional Refactoring in C# & ASP.NET
This is the forum to discuss the Wrox book Professional Refactoring in C# & ASP.NET by Danijel Arsenovski; ISBN: 9780470434529
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Refactoring in C# & ASP.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2009, 04:55 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default Chapter 13 : Querying Objects

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.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 - Forms as objects Zave BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 0 August 22nd, 2007 12:23 AM
Chapter 13 bwoll BOOK: Beginning Access 2003 VBA 1 June 7th, 2007 03:57 PM
Chapter 13 ElMorenito BOOK: Beginning ASP 3.0 0 January 14th, 2005 02:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.