1
Dim namePriceQuery = From prod In products
                     Select prod.Name, prod.Price

OR

Dim namePriceQuery = products.select(function(x) New With{ Key .newName =x.name _ 
, Key .newPrice= x.price})

To my knowledge the only difference is with with the second expression has the benefits of the Key keyword. What exactly is the difference between the two?? And what scenarios would I choose to use one or the other in? What are the pros and cons of one vs the other?

I am assuming the C# syntax equivalent will work the same and should be used in similar before mention scenarios. Thank you very much!

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
gh9
  • 10,169
  • 10
  • 63
  • 96
  • 1
    which itself links to yet another dupe. [Which LINQ syntax do you prefer? Fluent or Query Expression](http://stackoverflow.com/questions/214500/which-linq-syntax-do-you-prefer-fluent-or-query-expression) both have good answers. – gideon Apr 06 '11 at 19:31

2 Answers2

3

Either one works the same but I would choose the one that is most readable.

Evan Larsen
  • 9,935
  • 4
  • 46
  • 60
0

Both statements are equal. Linq queries formulated with linq syntax are translated into the appropriate method calls.

Select what fits you best in terms of readability and/or makes your code more understandable. If you get really complex queries, using select many and grouping the linq syntax is much better to read.

I switch between both forms of linq queries depending on the situation.

Zebi
  • 8,682
  • 1
  • 36
  • 42