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));
Timwi
  • 65,159
  • 33
  • 165
  • 230
  • Is index a member of your 'elem' class or an integer? – vc 74 Sep 21 '10 at 06:44
  • @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. – Timwi Sep 21 '10 at 06:56
  • 2
    @Timwi, what you call "LINQ syntax" is actually called "query comprehension syntax" – Thomas Levesque Sep 21 '10 at 13:03
  • @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*. – Timwi Sep 21 '10 at 13:57
  • [By Eric Lippert](http://blogs.msdn.com/b/ericlippert/archive/2009/12/07/query-transformations-are-syntactic.aspx), among others... I think Eric is a pretty good authority on this subject ;) – Thomas Levesque Sep 21 '10 at 14:01
  • @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? – Timwi Sep 21 '10 at 14:07
  • 1
    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 ;) – Thomas Levesque Sep 21 '10 at 14:10
  • I generally have seen it and refer to it as query expression vs. lambda expression. It's all LINQ. – Anthony Pegram Sep 21 '10 at 14:30

1 Answers1

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;
digEmAll
  • 56,430
  • 9
  • 115
  • 140