Is there a LINQ syntax for the (T, int) overloads of Where and Select?
-
04-10-2019 - |
Question
The query
var q = from elem in collection
where someCondition(elem)
select elem;
translates to
var q = collection.Where(elem => someCondition(elem));
Is there a LINQ syntax that would translate to the following?
var q = collection.Where((elem, index) => someCondition(elem, index));
Solution
No there's no LINQ syntax for that.
A simple work-around could be:
var q = from elem in collection.Select((x,i) => new {x,i})
where someCondition(elem.x,elem.i)
select elem.x;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow