TakeWhile, but get the element that stopped it also

后端 未结 3 606
栀梦
栀梦 2021-01-17 08:50

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 elem

3条回答
  •  再見小時候
    2021-01-17 09:37

    Just for fun:

    var a = new[] 
        {
            "two",
            "three",
            "four",
            "five",
        };
      Func predicate = item => item.StartsWith("t");      
      a.TakeWhile(predicate).Concat(new[] { a.SkipWhile(predicate).FirstOrDefault() })
    

提交回复
热议问题