diff --git a/MoreLinq/TagFirstLast.cs b/MoreLinq/TagFirstLast.cs index f22d3fbbf..2ffb5c9a3 100644 --- a/MoreLinq/TagFirstLast.cs +++ b/MoreLinq/TagFirstLast.cs @@ -60,8 +60,24 @@ public static IEnumerable TagFirstLast(this IEnumerab if (source == null) throw new ArgumentNullException(nameof(source)); if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector)); - return source.Index() // count-up - .CountDown(1, (e, cd) => resultSelector(e.Value, e.Key == 0, cd == 0)); + return _(); IEnumerable _() + { + using var enumerator = source.GetEnumerator(); + + if (!enumerator.MoveNext()) + yield break; + + var current = enumerator.Current; + var hasNext = enumerator.MoveNext(); + yield return resultSelector(current, true, !hasNext); + + while (hasNext) + { + current = enumerator.Current; + hasNext = enumerator.MoveNext(); + yield return resultSelector(current, false, !hasNext); + } + } } } }