如果您能够将整个查询拉到一个列表中,那么它就足够简单了:
var list = data.ToList();
var query = list.SkipWhile((item, index) => index + 2 < list.Count &&
item < list[index + 1] && list[index + 1] < list[index + 2]);
如果保持延迟执行很重要,那么问题就有点难了,你可以用这个函数来做:
public IEnumerable<TSource> NextTwoStrictlyIncreasing<TSource>(IEnumerable<TSource> source,
Func<TSource, TSource, bool> isStrictlyIncreasing)
{
using (var iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
yield break;
var twoBack = iterator.Current;
if (!iterator.MoveNext())
yield break;
var oneBack = iterator.Current;
bool previousIsIncreasing = false;
bool isIncreasing = false;
while (iterator.MoveNext())
{
isIncreasing = isStrictlyIncreasing(oneBack, iterator.Current);
if (isIncreasing && previousIsIncreasing)
{
yield return twoBack;
yield return oneBack;
yield return iterator.Current;
while (iterator.MoveNext())
yield return iterator.Current;
yield break;
}
twoBack = oneBack;
oneBack = iterator.Current;
previousIsIncreasing = isIncreasing;
}
}
}