1

I am a beginner in Linq.

How can I rewrite this lambda expression as a linq compiled query?

var query5 = CustomerList.Select((cust, index) =>  new {cust, index})
                         .Where(c =>  c.cust.Country == "USA"  &&  c.index  >  70)
                         .Select(c =>  new { c.cust.CustomerID, c.cust.CompanyName, 
                                            c.index });

like

var query5 = from c in .......
             where .....
             select c new {....}
4
  • 2
    That's not a "compiled" query. It's just a different syntax. It doesn't have a technical advantage. Commented Oct 13, 2009 at 7:23
  • I don't need technical advantage. Sorry! Commented Oct 13, 2009 at 7:25
  • I do feel the Linq syntax is more readable than the lambda variant. Commented Oct 13, 2009 at 7:29
  • @Jonathan van de Veen - maybe, but there are quite a few operations that are only available via the lambda syntax. Commented Oct 13, 2009 at 7:37

1 Answer 1

5

Well here's the closest query expression syntax:

var query5 = from c in CustomerList.Select((cust, index) =>  new {cust, index})
             where c.cust.Country == "USA"  &&  c.index  >  70
             select new { c.cust.CustomerID, c.cust.CompanyName, c.index };

Basically the one bit you can't do in query expression syntax is the "select including the index" overload. There simply isn't any syntax which translates to that. (The same goes for quite a few other operations - VB's LINQ syntax is richer in this respect, although personally I'm happy with the way that C# does it; it avoids adding too many contextual keywords.)

(As Mehrdad says, this isn't a "compiled" query. In fact the code will be compiled to exactly the same IL.)

Sign up to request clarification or add additional context in comments.

1 Comment

Wasn't aware of an overload that includes an index. Yet another linq mystery resolved :-D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.