Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting x number of elements from a System.Collections.Generic.List c#

Tags:

c#

list

What i need is a way to select the last 100 elements from a list, as list

    public List<Model.PIP> GetPIPList()
    {
        if (Repository.PIPRepository.PIPList == null)
            Repository.PIPRepository.Load();
        return Repository.PIPRepository.PIPList.Take(100);

    }

I get error like this

'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

like image 890
Taufiq Abdur Rahman Avatar asked Nov 30 '22 03:11

Taufiq Abdur Rahman


1 Answers

somelist.Reverse().Take(100).Reverse().ToList();

This would be much cheaper than ordering :) Also preserves the original ordering.

like image 133
leppie Avatar answered Dec 16 '22 03:12

leppie