Splice IEnumerable with Linq

Is there a built-in way to splice IEnumerable in Linq-To-Objects?

Sort of:

 List<string> options = new List<string>(); // Array of 20 strings List<string> lastOptions = options.Splice(1, 5); // Gets me all strings 1 through 5 into a new list 
+6
source share
1 answer

Try:

 List<string> options = new List<string>(); List<string> lastOptions = options.Skip(0).Take(5).ToList(); 

skip is used to show how to skip x elements (tnx drch)


You can create something like this: (extension method)

 public static class SpliceExtension { public static List<T> Splice<T>(this List<T> list, int offset, int count) { return list.Skip(offset).Take(count).ToList(); } } 

But this will only be available for listings.


You can also use it on IEnumerable<>

 public static class SpliceExtension { public static IEnumerable<T> Splice<T>(this IEnumerable<T> list, int offset, int count) { return list.Skip(offset).Take(count); } } 

Thus, List is not completely repeated.

use it like:

 List<string> lastOptions = options.Splice(1, 5).ToList(); 

I like it more because it can be used for multiple linq queries.

+16
source

Source: https://habr.com/ru/post/954783/