0

This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value. Source Code

public void Linq5() 
{ 
    string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 

    var shortDigits = digits.Where((digit, index) => digit.Length < index); 

    Console.WriteLine("Short digits:"); 
    foreach (var d in shortDigits) 
    { 
        Console.WriteLine("The word {0} is shorter than its value.", d); 
    } 
}

Now My Question is ...Can we write this in the LINQ Query format like:

from u in digits where u.Length>index select u;

Here How to get the INDEX value from above Query?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Tanya Samson
  • 21
  • 1
  • 6
  • 1
    possible duplicate of [LINQ where using index in query language](http://stackoverflow.com/questions/4049773/linq-where-using-index-in-query-language) – Ufuk Hacıoğulları Dec 20 '12 at 10:01
  • 1
    possible duplicate of http://stackoverflow.com/questions/4049773/linq-where-using-index-in-query-language – Teejay Dec 20 '12 at 10:01

2 Answers2

1

No, overloaded Where operator is not available in query syntax. Here is quote from msdn:

In query expression syntax, a where (Visual C#) or Where (Visual Basic) clause translates to an invocation of Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

You can introduce index manually:

int index = 0;
var query = from u in digits 
            where u.Length > index++ 
            select u;

But keep in mind that you should also manually reset index before each query invocation. Anyway - if you need index, then fluent API is the best choice for you:

var query = digits.Where((u, i) => u.Length > i); 

I don't see any reason why not to use it.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks...Is there any other option..? – Tanya Samson Dec 20 '12 at 16:58
  • @user1849388 I've added link to msdn, which points how query syntax translated into Where operator. Problem is that you should pass `Func` to where clause in query syntax. You can't add some additional parameter there. And I'm still don't understand why you don't want to use `Where((u, i) => u.Length > i)` – Sergey Berezovskiy Dec 20 '12 at 17:26
0

Try this:

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

var shortDigits = from pair in digits.Select((digit, index) => new { digit, index })
              where pair.digit.Length < pair.index
              select pair.digit;
Salaros
  • 1,444
  • 1
  • 14
  • 34
  • Thanks a lot. This is what exactly I'm looking for. btw, what is he difference between the Select and Where? I think both operators are doing the same work? – Tanya Samson Dec 21 '12 at 05:53
  • Select is usually used when you need to iterate ALL the elements of the enumeration and return something for those elements (in my example a pair of values). While Where is usually used to restrict the enumeration to a list of elements that satisfy a given condition. P.S. I my answer was useful for you consider accepting and upvoting it ;) – Salaros Dec 27 '12 at 10:20