Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of using "Select" multiple times in this LINQ query?

Tags:

vb.net

linq

I came across some code like this:

Dim results = From item In New List(Of Integer) From {1, 2, 3}
              Select item
              Select item

I am surprised that Select item twice is legal. It seems to behave exactly the same as if there was only one Select line. I tried converting to C# and it produces a compile error.

Is there any reason to use multiple Selects? Could this ever cause the query to behave differently?

like image 474
default.kramer Avatar asked May 29 '15 15:05

default.kramer


1 Answers

the C# equivalent syntax would be :

var results = from item in new List<int> {1, 2, 3}
              select item into item
              select item;

That way you create a new scope to "chain" queries or quoted from the VB.Net documentation (see links) The Select clause introduces a new set of range variables for subsequent query clauses (you can see the into C# keyword documention or the Select VB.Net clause documentation for further information and examples)

like image 196
Sehnsucht Avatar answered Nov 15 '22 06:11

Sehnsucht