5

I have LINQ expression like

var a = ctx.EntitySet
        .OrderByDescending(t => t.Property)
        .Skip(pageIndex * size) 
        .Take(size);

OrderBy() should call before Skip() and Take(), but sorting happens at the end. Can I solve this problem?

Sorry, many people didn't understand my question. Query runs without any errors, but I want

//It is I want
1) Sorting ALL data
2) Use Skip() and Take()

What I have in result if I do like at my example: 1) Skip() 2) Take() 3) Sorting only taked elements!

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
DraggonZ
  • 1,057
  • 1
  • 16
  • 22

3 Answers3

2

i don't know why , but somehow it works for me , hope it helps you

var a1 = from p in ctx.EntitySet
        .OrderByDescending(t => t.Property)
        select p;

var a2 = from p in a1
        .Skip(pageIndex * size) 
        .Take(size)
        select p;
Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
0

command orders in this sample is not Important. it will sort your data first and after that your data will be collected.

Mostafa Soghandi
  • 1,524
  • 1
  • 12
  • 20
0

have you tried this

if you go for below solution it will first get the records and than does sorting on that records which may lead you to wrong result.

var a = ctx.EntitySet
        .Skip(pageIndex * size) 
        .Take(size);

a = a.OrderByDescending(t => t.Property);

or

Following way you first doing sorting and than getting records after that, so by this way you can get the result you want , this is proper way to do

   var a = ctx.EntitySet
    .OrderByDescending(t => t.Property)
    .Skip(pageIndex * size) 
    .Take(size);

But it always depends on your requirement what you want....

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263