0

just got curious if this LINQ Statement:

  string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
  var shortDigits = digits.Where((digit, index) => digit.Length < index);

is convertible to this style of LINQ:

  var shortDigits  = from d in digits
                     ..........

If yes, can you please show me how...

Allan Chua
  • 9,305
  • 9
  • 41
  • 61
  • index is not a variable there. had you even tried this approach before? it was used as an alias for the index(position in array) parameter in the lambda expression. – Allan Chua Sep 12 '12 at 11:45
  • As far as I know, the query syntax doesn't support indexed where or select clauses. – Lee Sep 12 '12 at 11:51
  • @Lee is there any documentations that prove this wasn't supported on query syntax? I'd be happy to view this one.. – Allan Chua Sep 12 '12 at 11:52

1 Answers1

1

The problem you're having is that the overload of Where that you're using isn't supported in the fluent query syntax.

You can however get around this by performing a select first and projecting the index into your results like so:

var digitsWithIndex = digits.Select((d, i) => new { Digit = d, Index = i });

You can then consume the index in the fluent query syntax:

var query = from d in digitsWithIndex
            where d.Digit.Length < d.Index
            //just select the digit, we don't need the index any more
            select d.Digit;

This yields the results "five", "six", "seven", "eight", "nine".

I think in this situation I'd recommend sticking with using lambda syntax though, but that's my personal preference.

Here's the full code that I knocked up in LinqPad:

var digits = new []
{
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
};

var digitsWithIndex = digits.Select((d, i) => new { Digit = d, Index = i });

var query = from d in digitsWithIndex
            where d.Digit.Length < d.Index
            //just select the digit, we don't need the index any more
            select d.Digit;
Doctor Jones
  • 21,196
  • 13
  • 77
  • 99
  • +1 for you buddy.. It was nice to hear that the 2nd overload for where clause isn't supported in query syntax.. do have some references that specifies this limitation? – Allan Chua Sep 12 '12 at 11:55
  • 1
    I remember reading about it on Eric Lippert's blog, he gets questions all the time along the lines of "why doesn't the query comprehension syntax support feature x?". He stated that they wanted to keep the language clean by not introducing too many new keywords. I've been trying to find the specific article to link to, but my Google-Fu is letting me down. I'll have another go after lunch :-) – Doctor Jones Sep 12 '12 at 12:02
  • You're welcome. I can't find it on his blog so it could have been a comment or answer on SO. I remember him saying that the VB team made a design choice to implement keywords for more LINQ operators than the C# team. They were keen for C# to remain "pure" wheras VB is a different type of language. – Doctor Jones Sep 12 '12 at 13:01