Can we control LINQ expression order with Skip(), Take() and OrderBy()

后端 未结 6 775
日久生厌
日久生厌 2021-02-08 14:16

I\'m using LINQ to Entities to display paged results. But I\'m having issues with the combination of Skip(), Take() and OrderBy() calls.

6条回答
  •  孤街浪徒
    2021-02-08 15:02

    Are you absolutely certain the ordering is off? What does the SQL look like?

    Can you reorder your code as follows and post the output?

    // Redefine your queries. 
    var query = ctx.EntitySet.Where(/* filter */).OrderBy(e => e.ChangedDate); 
    var skipped = query.Skip(n).Take(x);
    
    // let's look at the SQL, shall we?
    var querySQL = query.ToTraceString();
    var skippedSQL = skipped.ToTraceString();
    
    // actual execution of the queries...
    int total = query.Count(); 
    var result = skipped.ToList(); 
    

    Edit:

    I'm absolutely certain. You can check my "edit" to see trace result of my query with skipped trace result that is imperative in this case. Count is not really important.

    Yeah, I see it. Wow, that's a stumper. Might even be an outright bug. I note you're not using SQL Server... what DB are you using? Looks like it might be MySQl.

提交回复
热议问题