How to do Array Slicing Using LINQ?
To understand this we will take an array of 10 elements and out of which we need to return five of them, with the intention to return the rest of them on demand. And it is known as pagination and it is achieved by using two mainly methods of LINQ that are Skip() and Take(). So import LINQ and use two of these methods as in below given example:-
var objElements=new string[] {"E1","E2","E3","E4","E5","E6","E7","E8","E9","E10"};
var objSliced=objElements.Skip(0).Take(5);
foreach (var obj in objSliced)
Console.WriteLine(obj);
In above code we pass number of element we want to skip is pass to Skip() method. And this will turns out to be starting index as we put 0 with Skip methods. And in second method is Take() here we pass the number of elements which we want to include in Take() method. And this operation returns a new IEnumerable that we can enumerate. | |