C# linq order by and take

12,606

Solution 1

Yes, very much so. It's an order of operations type thing.

Say you have a data set of

A
F
B
D
F
C

and you do a return query.Take(3).OrderBy(o => o.name); you would get

A
B
F

However, if you do return query.OrderBy(o => o.name).Take(3); then you would get

A
B
C

Solution 2

The difference between the two queries is that the first one will produce the initial 15 items from the list ordered by id in descending order, while the second one will take the initial 15 items in "natural" order, re-order them in descending order by id, and return back to you.

Since the "natural" order in RDBMS is not guaranteed to remain the same across multiple requests, only the first query will return consistent results. In fact, the second query could potentially return entirely different record sets each time you invoke it.

Results of both queries would be consistent if you do them in-memory.

The difference is easy to illustrate with a simple example of 100 records with ids from 1 to 100, inclusive, ordered in ascending order by their ID. The first query would return records with IDs 100, 99, 98, ..., 86, while the second query would return records with IDs 15, 14, 13, ..., 1.

Solution 3

Yes, those two queries will return different results, as:

  • query.OrderByDescending(o => o.id).Take(15) will sort the whole source query and then return the first 15 items. This will result in the first 15 items across the whole sorted dataset you're using.
  • query.Take(15).OrderByDescending(o => o.id) will take the first 15 items in the query (in their current order) and just sort those first 15 items (so this is probably not what you want).

Solution 4

Yes. One takes 15 and orders them. The other order them and takes 15.

using System.Linq;

public static void Main(string[] args)
{
    var nums = Enumerable
        .Range(0, 50)
        .OrderBy(o => Guid.NewGuid().GetHashCode())  // poor mans shuffle
        .ToList(); 

    Console.WriteLine(string.Join(",", nums));

    Console.WriteLine(string.Join(",", nums.Take(15).OrderBy(i => i)));

    Console.WriteLine(string.Join(",", nums.OrderBy(i => i).Take(15)));

    Console.ReadLine();
}

Output:

6,32,22,18,9,11,5,33,0,24,1,42,38,30,21,20,23,2,36,8,15,12,29,47,46,19,49,44,4,3
    7,40,3,10,41,34,17,31,16,43,35,39,25,27,45,7,28,14,13,26,48

0,1,5,6,9,11,18,21,22,24,30,32,33,38,42 // nums.Take(15).OrderBy(i => i)

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14      // nums.OrderBy(i => i).Take(15)

Kindof simple to test ...

Share:
12,606
marco burrometo
Author by

marco burrometo

I am Marco Burrometo Born in '92. Based in northern Italy. My greatest passions are Technology and Music. I like to put user experience in first place and focus on customer's needs to archieve the best results. I have fun creating unique things and keeping myself updated on newest technologies. Bass guitar player since I was 15 🎸 Vinyl addicted πŸŽ›πŸ“» Proud dachshund owner and lover 🐢🦴

Updated on June 12, 2022

Comments

  • marco burrometo
    marco burrometo almost 2 years

    I'm doing a query in C# linq.

    After selecting and saving the query i have to do sort it and take 15 rows...

    var query = from a 
    [...]
    select new
    {
        a.id,
        a.desc,
    }
    [...]
    

    Are there any different between doing this:

    return query.OrderByDescending(o => o.id).Take(15);
    

    and this?

    return query.Take(15).OrderByDescending(o => o.id);
    

    Thank you very much!