Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:55:03+00:00 2026-06-12T04:55:03+00:00

Which query has better performance to return an object which has the max value

  • 0

Which query has better performance to return an object which has the max value for a specific property?

 var i = from item in listOfItems
         orderby item.Number descending
         select item;

or:

 var i = from item in listOfItems
          where item.Number== (from l in listOfItems select item).Max(l => l.Number)
          select item;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:55:05+00:00Added an answer on June 12, 2026 at 4:55 am

    In SQL, the two queries are translated into (at least, if you do a .FirstOrDefault() in the resulting IEnumerables to select the wanted object):

    SELECT TOP (1) [t0].[Number]
    FROM [Item] AS [t0]
    ORDER BY [t0].[Number] DESC
    
    SELECT TOP (1) [t0].[Number]
    FROM [Item] AS [t0]
    WHERE [t0].[Number] = ((
        SELECT MAX([t1].[Number])
        FROM [Item] AS [t1]
        ))
    

    I can’t say which would be faster based on anything factual, but I’d go with the first order-by solution.

    -* Edited to address querying the database *-

    If you are querying an in memory collection, the difference should be very small.

    However, here is the results of an entirely unscientific test by querying 100.000 semirandom numbers in the two different ways of querying each, 10 times:

    By desc (0): 00:00:00.0173879
    By .Max (0): 00:00:00.0132833
    
    By desc (1): 00:00:00.0250781
    By .Max (1): 00:00:00.0140374
    
    By desc (2): 00:00:00.0073955
    By .Max (2): 00:00:00.0111658
    
    By desc (3): 00:00:00.0066200
    By .Max (3): 00:00:00.0115127
    
    By desc (4): 00:00:00.0071220
    By .Max (4): 00:00:00.0119572
    
    By desc (5): 00:00:00.0070341
    By .Max (5): 00:00:00.0114320
    
    By desc (6): 00:00:00.0066670
    By .Max (6): 00:00:00.0111127
    
    By desc (7): 00:00:00.0071905
    By .Max (7): 00:00:00.0116715
    
    By desc (8): 00:00:00.0065414
    By .Max (8): 00:00:00.0118076
    
    By desc (9): 00:00:00.0071662
    By .Max (9): 00:00:00.0131962
    

    Here is the entire LINQPad script:

    void Main()
    {
        var listOfItems = new List<Item>();
    
        // Make 100000 Items with semirandom numbers
        for(int i=0; i<100000; i++)
        {
            listOfItems.Add(new Item { Number = i * DateTime.Now.Ticks });
        }
    
        for(int i=0; i<10; i++)
        {
            RunTest(i, listOfItems);
        }
    }
    
    void RunTest(int count, List<Item> listOfItems)
    {
        var timer = new System.Diagnostics.Stopwatch();
        timer.Start();
        // Find by descending
        for(int i=0; i<100000; i++)
        {
            var itemWithLargestNumber = 
                from item in listOfItems
                orderby item.Number descending
                select item;
        }
        timer.Stop();
        string.Format("By desc ({0}): {1}", count, timer.Elapsed).Dump();
    
        timer = new System.Diagnostics.Stopwatch();
        timer.Start();
    
        // Find by .Max
        for(int i=0; i<100000; i++)
        {
            var itemWithLargestNumber = 
                from item in listOfItems
                where item.Number== (from l in listOfItems select item).Max(l => l.Number)
                select item;
        }
        timer.Stop();
        string.Format("By .Max ({0}): {1}", count, timer.Elapsed).Dump();
    
        "".Dump();
    }
    
    class Item 
    {
        public long Number { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which one of below query has better performance? What's the upside/downside for both query?
I'm wondering which way to get data from a MySQL database has better performance
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
Which one is more reliable and has better performance? Setting MySQL unique key and
I have a result of a query which has BLOB column defined in it.
I have code which performs query on Jboss server. It has JNDI based datasource
using php, i have a sql query which works fine, dateentry only has the
I have query table copyQuery which has Soil Condition has one of the columns,
I need to query a Sybase database which has a lot of data in
I have Db which has data of an year...so I need a query to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.