locked
Just For Fun - Why must you use .OrderBy to use the .Skip method? Technical, theoretical, or completely arbitrary decision? RRS feed

  • Question

  • User-1851576699 posted

    I am curious about the rationale for mandating a call to OrderBy in LINQ to Entities. On one hand, I understand the concept of not being able to skip elements if you don't know what they are to begin with. On the other hand, it seems like some conventions could have been implemented for some circumstances where ordering of the elements is completely arbitrary from the end-developers point of view. I guess what I am saying is that there is a default order to a System.Data.Entity.DbSet<> of elements. And that is the physical order that they exists in the database. There is a default order for an array of System.Objects[] in memory and that is the memory address from low to high, or the array index from low to high.

    Now, if your IQueryable has any type of filters on it at the point at which the .Skip() method is called then it makes perfect sense to say, "Hey, how can I skip elements that haven't even been selected yet".  But if you wrote something along the lines of:

    context.Users.Skip(10).Take(5);

    Where there is no filtering then this seems like an acceptable way to use the .Skip and .Take methods. How do you call .SortBy method on a, for example, 100 entities when the selector your using to .SortBy is the same value for every element? 

    For example, if I did have a .Where() filter on an System.Linq.IQueryable that pulled records based upon a System.Boolean of isMemberOfClub, then 100 members could be pulled and calling .SortBy() on that set of entities would be pointless using the isMemberOfClub field as a sort key. What's the practical gain? It changes nothing.

    I guess all I am trying to figure out is why .OrderBy was made a mandatory pre-requisite of calling .Skip when it doesn't seem absolutely necessary in all cases. It seems to be simply jumping through hoops without any gain in the end result.

    Sunday, September 13, 2015 4:52 PM

Answers

  • User-821857111 posted

    Skip and Take gets translated to various flavours of  SQL ( e.g. Row_Number or Offset and Fetch) which invariably require an Order By clause to work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 13, 2015 5:08 PM
  • User753101303 posted

    If I remember SQL Server allow that and if not technical it's at least a logical issue.

    Skip(10) means "skip the first 10 rows" but how do you define which rows are coming first if no order by clause is specified? In SQL, if you have no order by clause the db is free to return rows in whatever order he wants and even free to no always use the same order from one query to another.

    So even if this not technically required, it makes very little sense to use things such as Skip(10) without an order by clause as then you can't be 100%% sure you'll have the rows you expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2015 12:21 PM

All replies

  • User-821857111 posted

    Skip and Take gets translated to various flavours of  SQL ( e.g. Row_Number or Offset and Fetch) which invariably require an Order By clause to work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 13, 2015 5:08 PM
  • User-1851576699 posted

    That it explains I guess. So, it's a technical formality. I am not sure that's a good thing as far as code readability and comprehension goes. After all, it's what caused me to question it's mandatory use. None the less, I accept it and I march forward. :)

    Tuesday, September 15, 2015 6:39 PM
  • User-821857111 posted

    I am not sure that's a good thing as far as code readability and comprehension goes.
    It's required to guarantee consistency of results. 

    Wednesday, September 16, 2015 12:12 PM
  • User753101303 posted

    If I remember SQL Server allow that and if not technical it's at least a logical issue.

    Skip(10) means "skip the first 10 rows" but how do you define which rows are coming first if no order by clause is specified? In SQL, if you have no order by clause the db is free to return rows in whatever order he wants and even free to no always use the same order from one query to another.

    So even if this not technically required, it makes very little sense to use things such as Skip(10) without an order by clause as then you can't be 100%% sure you'll have the rows you expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2015 12:21 PM