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

I came across this code:

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

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

Is there any reason to use multiple samples? Could this ever make a request behave differently?

+6
source share
3 answers

C # syntax is equivalent to:

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

Thus, you create a new area for the "chain" of requests or is cited in the documentation of VB.Net (see links). The Select clause introduces a new set of range variables for subsequent query clauses (you can see the C # documention keyword or the Select VB.Net section for more information and examples)

+5
source

I can’t say why this is allowed, but if you look at the compiled IL code, you will see that the expression ends with a call to Enumerable.Select twice.

 L_0021: ldftn int32 ConsoleApplication1.Module1::_Lambda$__1(int32) L_0027: newobj instance void [mscorlib]System.Func`2<int32, int32>::.ctor(object, native int) L_002c: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!1> [System.Core]System.Linq.Enumerable::Select<int32, int32>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, !!1>) L_0032: ldftn int32 ConsoleApplication1.Module1::_Lambda$__2(int32) L_0038: newobj instance void [mscorlib]System.Func`2<int32, int32>::.ctor(object, native int) L_003d: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!1> [System.Core]System.Linq.Enumerable::Select<int32, int32>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, !!1>) 

So, in the end, the request is equal to this:

 Dim results = {1, 2, 3}.Select(Function(i) i).Select(Function(i) i) 
+4
source

The answers from Sehnsucht and Bjørn-Roger Kringsjå are accurate. The bottom line is that you use Select to create a result set, and then using Select again create a new result set based on the previous one. In your example, both result sets are identical, so this is the same as if you only used Select once.

To answer the second question, there may be times when you want to write your query more than using Select to facilitate readability or for some other reason. A trivial example might look like this:

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

which will return a set containing only the integer 2. Obviously, this can be written with one Select and one Where (using And ), but there may be times when your conditions are so complex that you want to separate them and not make them part of one sentence Where . Or there may be other things you want to do that require you to split the application into smaller parts.

Generally speaking, this kind of thing is of little use, but it is good that LINQ allows this if you have a need.

+3
source

Source: https://habr.com/ru/post/988179/


All Articles