Search code examples
c#razor-pages

How to Get the last 3 added value in specific column in my database


my data table name is called Shift its data is like below

enter image description here

i use below syntax to get the last added value

Shiftlast = _db.Shift.OrderByDescending(c => c.Id).First(); ---the result is 3500

my question is how to get also the before last and before before last values also which is 2500 and and 152,i need each value to be get in variable as below

Shiftlast = _db.Shift.OrderByDescending(c => c.Id).First();
Shiftbeforelast = _db.Shift.OrderByDescending(c => c.Id).??;
Shiftbeforebeforelast = _db.Shift.OrderByDescending(c => c.Id).??;

i tried take(3) method but i get error enter image description here


Solution

  • What type of variable is Shifts? Maybe is List<T> and you need to add .ToList();

    Shifts = _db.Shift.OrderByDescending(c => c.Id).Take(3).ToList();