Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a section out of the middle of a list in C#

Tags:

c#

.net

linq

I have a list in C#. Given two number - the starting position and the number of records - how can I select from the middle of a list? What kind of collection should I use?

E.g. Starting position = 10. Number of records = 20. Total number of records in list = 50. I want to get back the objects in elements 10 to 29.

like image 649
dnatoli Avatar asked Nov 16 '11 04:11

dnatoli


3 Answers

Assuming you're using .NET 3.5:

using System.Linq;

list.Skip(10).Take(20)
like image 70
Joe White Avatar answered Sep 26 '22 00:09

Joe White


Something like list1.Skip(10).Take(20) should work for you

like image 38
V4Vendetta Avatar answered Sep 27 '22 00:09

V4Vendetta


use the LINQ extension methods skip() and take()

var myList = getList();
var middle = myList.Skip(10).Take(20);
like image 37
Jason Avatar answered Sep 26 '22 00:09

Jason