I've often found myself missing (and implementing) an IEnumerable<T>.Do method.
Just like ForEach, only it yields the sequence. Great for when you want to
insert a side effecting action in the middle of a linq statement.
Would be nice if this was implemented once in a central location. :)
Implementation is trivial, just throw a yield into a copy of ForEach:
public static IEnumerable<T> Do<T>(this IEnumerable<T> source, Action<T> action)
{
source.ThrowIfNull("source");
action.ThrowIfNull("action");
foreach (T element in source)
{
action(element);
yield return element;
}
}
Original issue reported on code.google.com by roj...@gmail.com on 2 Aug 2011 at 7:03
Original issue reported on code.google.com by
roj...@gmail.com
on 2 Aug 2011 at 7:03