Of course. But first, note that I recommend against this; I say that the sequence operator should not have a side effect, and the operator should have a side effect. If you are doing something in this ForEach lambda, make this statement in the body of the foreach loop, rather than making it look like a sequence statement.
So here is what you do. First, you write yourself ForEach, which runs on arbitrary sequences, not just lists:
public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action) { foreach(var item in sequence) action(item); }
And now you write your break as follows:
someList .TakeWhile(x=>x.ToString() != "foo") .ForEach(sl=> {});
Eric Lippert
source share