I have an IEnumerable
Similar to Marc's answer, but you could write an extension method to wrap it up.
public static class LastEnumerator
{
public static IEnumerable> GetLastEnumerable(this IEnumerable blah)
{
bool isFirst = true;
using (var enumerator = blah.GetEnumerator())
{
if (enumerator.MoveNext())
{
bool isLast;
do
{
var current = enumerator.Current;
isLast = !enumerator.MoveNext();
yield return new MetaEnumerableItem
{
Value = current,
IsLast = isLast,
IsFirst = isFirst
};
isFirst = false;
} while (!isLast);
}
}
}
}
public class MetaEnumerableItem
{
public T Value { get; set; }
public bool IsLast { get; set; }
public bool IsFirst { get; set; }
}
Then call it like so:
foreach (var row in records.GetLastEnumerable())
{
output(row.Value);
if(row.IsLast)
{
outputLastStuff(row.Value);
}
}