0

I have a list:

List<BtnCountViews> btnCountViewsList;

The BtnCountViews class looks like this:

public class BtnCountViews
{
    public int DayOfYear { get; set; }
    public int BtnCount { get; set; }
    public int Views { get; set; }
}

The data has been added to the list in a sorted order already.

Can someone tell me how I can get just the last 50 elements? I am hoping it's possible with LINQ but I have not seen any examples on how to do this.

Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

3

Just skip the first list's size minus 50.

        var list = new List<BtnCountViews>();
        for (int i = 0; i < 100; i++)
        {
            list.Add(new BtnCountViews() { BtnCount = i, DayOfYear = i, Views = i });
        }

        var last50 = list.Skip(list.Count - 50).ToList();
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28