c# - Best way to get range from 1 to n-1 with linq -
i use linq query sorted list n elements, can elemnts between first , last.
i have tried following, seems not good:
var result1 = mylist.skip(1).take(mylist.count()-2); var result2 = mylist.getrange(1, mylist.count()-2);
are there other ways achieve goal?
you have take account linq perform many loops necessary achieve purpose. sometimes, writing own loop gets better results. encapsulate loop extension methods , 1 line solution:
namespace system.linq { public static class mylinqextensions { public static ienumerable<t> range<t>(this ienumerable<t> input, int start, int end) { int = 0; foreach(var item in input) { if(i < start) continue; if(i > end) break; yield return item; i++; } } } }
Comments
Post a Comment