LINQ Take & Skip

By Riley K

In some cases we want to Skip few items and take the remaining like in a List LINQ offers a greate way using Take() and Skip() Methods

var list = new List<int>();
            list.Add(2);
            list.Add(3);
            list.Add(5);
            list.Add(7);
            list.Add(57);
            list.Add(47);
            list.Add(17);
            list.Add(27);

From the above list I want to skip the first 2 items and take the remaining.

var result = from res in list.Skip<int>(2).Take<int>(5)
                           select r
es;

 foreach (int r in result)
 {
     Console.WriteLine("{0}", r.ToString());
 
}

LINQ Take & Skip  (2020 Views)