You could just write your own Take
and Skip
public static IEnumerable<T> Skip<T>(IEnumerable<T> set, int count)
{
if(set == null) throw ArgumentNullException("set");
if(count < 0) throw ArgumentOutOfRangeException("count");
return SkipImp(set, count);
}
private static IEnumerable<T> SkipImp<T>(IEnumerable<T> set, int count)
{
foreach(var item in set)
{
if(count-- <= 0)
{
yield return item;
}
}
}
public static IEnumerable<T> Take<T>(IEnumerable<T> set, int count)
{
if(set == null) throw ArgumentNullException("set");
if(count < 0) throw ArgumentOutOfRangeException("count");
return TakeImp(set, count);
}
private static IEnumerable<T> TakeImp<T>(IEnumerable<T> set, int count)
{
foreach(var item in set)
{
if(count-- > 0)
{
yield return item;
}
else
{
yield break;
}
}
}
You'd have to do
var results = ContainingClass.Take(ContainingClass.Skip(list, 3), 4);
instead of
var results = list.Skip(3).Take(4);
Note the reason for two separate methods in each case is so the exceptions will be thrown when the method is called rather than when the IEnumerable<T>
is iterated.
I think you can even make them extension methods if you use a compiler that supports that (VS 2008 and up), as it's a feature of the compiler version and not the .net version.