I'd like to use the LINQ TakeWhile
function on LINQ to Objects. However, I also need to know the first element that "broke" the function, i.e. the first element where the condition was not true.
Is there a single function to get all of the objects that don't match, plus the first that does?
For example, given the set {1, 2, 3, 4, 5, 6, 7, 8}
,
mySet.MagicTakeWhile(x => x != 5);
=> {1, 2, 3, 4, 5}
LINQ to Objects doesn't have such an operator. But it's straightforward to implement a
TakeUntil
extension yourself. Here's one such implementation from moreLinq.I think you can use SkipWhile, and then take the first element.
UPDATE
If you want a single extension method, you can use the following:
Just for fun: