var item = list.Where(t => somecondition);
I would love to be able to find out the index of the element that was returned, in fact, in my case a
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
.