Is there a LINQ syntax for the (T, int) overloads of Where and Select?
Asked Answered
D

1

18

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));
Dowski answered 21/9, 2010 at 6:27 Comment(8)
Is index a member of your 'elem' class or an integer?Nerland
@vc 74: Several ways of answering your question... ① Move the cursor over the word Where and press F12. ② Look at the title of this question.Dowski
@Timwi, what you call "LINQ syntax" is actually called "query comprehension syntax"Plumbism
@Thomas: By who? The top Google search results for that bring up nothing on microsoft.com, msdn.com, or anything related to C# language design. The C# language specification calls them query expressions.Dowski
By Eric Lippert, among others... I think Eric is a pretty good authority on this subject ;)Plumbism
@Thomas: While I respect Eric Lippert, I think the C# language specification is a bit more authoritative. You will notice that he used quotation marks, probably because he was aware that this is not the “real” term. Furthermore, can you find more than one single blog entry?Dowski
Not much more authoritative, seeing that the actual compiler doesn't always follow to the spec ;). Anyway, while it might not be the "official" term for it, it is definitely not called "LINQ syntax"... so we're both wrong, I guess ;)Plumbism
I generally have seen it and refer to it as query expression vs. lambda expression. It's all LINQ.Giguere
F
17

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;
Fleabite answered 21/9, 2010 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.