Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take only entries from even positions in List

Tags:

c#

.net

linq

How I can select using Linq only entries from even positions in a list ?

like image 884
Night Walker Avatar asked Apr 20 '12 17:04

Night Walker


1 Answers

You can use the overload to Enumerable.Where in which the predicate includes the item's index.

var myList = new List<int>{ 1, 2, 3, 4, 5, 6 };
var evenIndexes = myList.Where( (num, index) => index % 2 == 0);
like image 185
goric Avatar answered Oct 09 '22 12:10

goric