Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want take the last 4 record but always get the first 4

I have this in my method:

var qry = db.Forms.Take(4)
                  .Where(m => m.SateliteID == Id)
                  .OrderByDescending(m => m.Tanggal)
                  .ToArray();

What I want is getting the last 4 records from all the records available, but what I get is the first 4 records. What I have done wrong? I thought that command will be the same with this:

SELECT TOP 4 <fields> FROM Forms WHERE sateliteID = Id
ORDER BY tanggal DESC

But it seems they are a different. What should I do to get what I want (the last 4 records instead of the first 4 records)? Thanks in advance for the help.

like image 552
Tanu Avatar asked Nov 30 '22 23:11

Tanu


2 Answers

Move your Take:

var qry = db.Forms.Where(m => m.SateliteID == Id)
                 .OrderByDescending(m => m.Tanggal)
                 .Take(4)
                 .ToArray();
like image 132
devdigital Avatar answered Dec 04 '22 10:12

devdigital


var qry = db.Forms.Where(m => m.SateliteID == Id).OrderByDescending(m => m.Tanggal).Take(4).ToArray();

You should order and than take some results from ordered list. Methods should be called in that kind of order.

like image 41
MarcinJuraszek Avatar answered Dec 04 '22 09:12

MarcinJuraszek