Get only last string values ​​from a list in asp.net

I only want to get the last line of the list. Is it possible? My list is database related. It can return different rows each time. I just want to get the last line. Since I don't know how to do this. I haven't tried anything

+3


source to share


2 answers


var lastItem =  ListView1.Items.LastOrDefault();

      



0


source


You can try Linq (the only trick is: OfType

or Cast

):

var result = myListView.Items
  .OfType<Object>() //TODO: out your type here
  .LastOrDefault(); // last row or null (if myListView is empty)

      



Or straight

var result = myListView.Items.Count > 0          // do we have any rows? 
  ? myListView.Items[myListView.Items.Count - 1] // if yes, get the last one 
  : null;                                        // null on empty list view

      

0


source







All Articles