Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# linq order by and take

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!

like image 433
marco burrometo Avatar asked Jan 17 '18 15:01

marco burrometo


1 Answers

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
like image 119
gilliduck Avatar answered Oct 18 '22 22:10

gilliduck