Finding first index of element that matches a condition using LINQ
If you really just need the first index then count the ones that don't match:
var index = list.TakeWhile(t => !someCondition).Count()
I'd need more context, but if you're just getting an index so that you can call .Skip
, I would recommend taking a look at .SkipWhile
.
If you do really need the index, I'd suggest writing your own .IndexOf
extension method.
Sure, it's pretty easy:
var index = list.Select((value, index) => new { value, index = index + 1 })
.Where(pair => SomeCondition(pair.value))
.Select(pair => pair.index)
.FirstOrDefault() - 1;
That will return the index if it finds anything matching, or -1 otherwise. The +1 and -1 is to get the behaviour for the case where there are no matches. If you knew that there would always be a match, it would be simpler:
var index = list.Select((value, index) => new { value, index })
.Where(pair => SomeCondition(pair.value))
.Select(pair => pair.index)
.FirstOrDefault();
If you're happy to get the rest of the list from that point onwards, SkipWhile
is definitely your friend, as mentioned by Chris. If want the rest of the list and the original index, that's easy too:
var query = list.Select((value, index) => new { value, index })
.SkipWhile(pair => !SomeCondition(pair.value))
That will give you a sequence of { value, index }
pairs from the first value matching SomeCondition
.