Linq "into" and "let" equally???
Collapse
This topic is closed.
X
X
-
Jon Skeet [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.ne twrote:Do you mean "do they do the same thing"? If so, do you meanLinq "into" and "let" equally???
"select ... into" rather than "group ... into"?
I'll assume you mean "select ... into". They're not quite the same.
When you do "select ... into" you have a *single* range variable at
that point; any previous range variables are gone, basically.
If you do "let" then it just introduces a *new* range variable. So,
for instance, you could do:
var query = from x in Enumerable.Rang e(0, 10)
let y = x*2
select x+y;
But if you do:
var query = from x in Enumerable.Rang e(0, 10)
select x*2 into y
select x+y;
it will fail, because at the "x+y" stage "x" is no longer available.
(I haven't tried the above code yet - I'm about to do so when I've
installed 3.5 onto my laptop.)
Jon
-
Marc Gravell -
Frans Bouma [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
Jon Skeet [C# MVP] wrote:
With queries like that, I always wonder why on earth would anyone wantOn Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.ne twrote:>Linq "into" and "let" equally???
Do you mean "do they do the same thing"? If so, do you mean
"select ... into" rather than "group ... into"?
>
I'll assume you mean "select ... into". They're not quite the same.
When you do "select ... into" you have a single range variable at
that point; any previous range variables are gone, basically.
>
If you do "let" then it just introduces a new range variable. So,
for instance, you could do:
>
var query = from x in Enumerable.Rang e(0, 10)
let y = x*2
select x+y;
>
But if you do:
>
var query = from x in Enumerable.Rang e(0, 10)
select x*2 into y
select x+y;
to use that construct in that way...
I mean: why would anyone use select <expressionin to <var? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Comment
-
Jon Skeet [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
On Nov 23, 9:27 am, "Frans Bouma [C# MVP]"
<perseus.usenet NOS...@xs4all.n lwrote:It's a good indicator that the only thing you care about from the>But if you do:>var query = from x in Enumerable.Rang e(0, 10)
select x*2 into y
select x+y;
With queries like that, I always wonder why on earth would anyone want
to use that construct in that way...
>
I mean: why would anyone use select <expressionin to <var? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
previous query is the actual result of the select - no intermediate
stuff needs to be propagated. (This could also make things a bit more
efficient in some cases, although that's rarely an issue.)
I'd personally write it as two queries:
var firstQuery = from x in source where blah select foo;
var secondQuery = from bar in firstQuery where ...;
which is basically the same thing, but a bit clearer IMO. There's room
for personal preference though :)
Jon
Comment
-
Alexander Vasilevsky
Re: Linq "into" ; and "let&qu ot; equally???
Thank You! Very good example!
http://www.alvas.net - Audio tools for C# and VB.Net developers
"Jon Skeet [C# MVP]" <skeet@pobox.co m???????/???????? ? ????????
?????????:
news:c048bac5-fd02-46ff-a19d-9a0a270d9e5f@o4 2g2000hsc.googl egroups.com...On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.ne twrote:>>Linq "into" and "let" equally???
Do you mean "do they do the same thing"? If so, do you mean
"select ... into" rather than "group ... into"?
>
I'll assume you mean "select ... into". They're not quite the same.
When you do "select ... into" you have a *single* range variable at
that point; any previous range variables are gone, basically.
>
If you do "let" then it just introduces a *new* range variable. So,
for instance, you could do:
>
var query = from x in Enumerable.Rang e(0, 10)
let y = x*2
select x+y;
>
But if you do:
>
var query = from x in Enumerable.Rang e(0, 10)
select x*2 into y
select x+y;
>
it will fail, because at the "x+y" stage "x" is no longer available.
>
(I haven't tried the above code yet - I'm about to do so when I've
installed 3.5 onto my laptop.)
>
Jon
Comment
-
Alexander Vasilevsky
Re: Linq "into" ; and "let&qu ot; equally???
Why painful?
http://www.alvas.net - Audio tools for C# and VB.Net developers
"Frans Bouma [C# MVP]" <perseus.usenet NOSPAM@xs4all.n l???????/???????? ?
???????? ?????????: news:xn0fe1a3l4 2cy3000@news.mi crosoft.com...Jon Skeet [C# MVP] wrote:
>>>On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.ne twrote:>>Linq "into" and "let" equally???
>Do you mean "do they do the same thing"? If so, do you mean
>"select ... into" rather than "group ... into"?
>>
>I'll assume you mean "select ... into". They're not quite the same.
>When you do "select ... into" you have a single range variable at
>that point; any previous range variables are gone, basically.
>>
>If you do "let" then it just introduces a new range variable. So,
>for instance, you could do:
>>
>var query = from x in Enumerable.Rang e(0, 10)
>let y = x*2
>select x+y;
>>
>But if you do:
>>
>var query = from x in Enumerable.Rang e(0, 10)
>select x*2 into y
>select x+y;
With queries like that, I always wonder why on earth would anyone want
to use that construct in that way...
>
I mean: why would anyone use select <expressionin to <var? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
>
FB
>
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Comment
-
Frans Bouma [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
Alexander Vasilevsky wrote:
because it keeps people in an imperative mindset, which means thatWhy painful?
they can't use the set-oriented functionality in full potential.
Perfect examples are people who write long pieces of SQL with cursors
because they don't understand how set-oriented languages work.
FB>
>
"Frans Bouma [C# MVP]" <perseus.usenet NOSPAM@xs4all.n l>
???????/???????? ? ???????? ?????????:
news:xn0fe1a3l4 2cy3000@news.mi crosoft.com...Jon Skeet [C# MVP] wrote:
With queries like that, I always wonder why on earth would anyone>On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.ne twrote:
>Linq "into" and "let" equally???
>
Do you mean "do they do the same thing"? If so, do you mean
"select ... into" rather than "group ... into"?
>
I'll assume you mean "select ... into". They're not quite the
same. When you do "select ... into" you have a single range
variable at that point; any previous range variables are gone,
basically.
>
If you do "let" then it just introduces a new range variable. So,
for instance, you could do:
>
var query = from x in Enumerable.Rang e(0, 10)
let y = x*2
select x+y;
>
But if you do:
>
var query = from x in Enumerable.Rang e(0, 10)
select x*2 into y
select x+y;
want to use that construct in that way...
I mean: why would anyone use select <expressionin to <var? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Comment
-
Jon Skeet [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
Frans Bouma [C# MVP] <perseus.usenet NOSPAM@xs4all.n lwrote:I don't see how it keeps people in an imperative mindset. It allows>Why painful?
because it keeps people in an imperative mindset, which means that
they can't use the set-oriented functionality in full potential.
Perfect examples are people who write long pieces of SQL with cursors
because they don't understand how set-oriented languages work.
them to express that the sequence which is the result of the "select"
is all they need for the next part of their query.
LINQ isn't actually set-oriented - it's sequence-oriented, a pipeline.
It may be convenient to think in terms of sets when writing LINQ to
SQL, but LINQ to Objects should *definitely* be considered in terms of
sequences.
--
Jon Skeet - <skeet@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Comment
-
Frans Bouma [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
Jon Skeet [C# MVP] wrote:
A query which is executed on a db, will end up as a set-orientedFrans Bouma [C# MVP] <perseus.usenet NOSPAM@xs4all.n lwrote:>because it keeps people in an imperative mindset, which means thatWhy painful?
they can't use the set-oriented functionality in full potential.
Perfect examples are people who write long pieces of SQL with
cursors because they don't understand how set-oriented languages
work.
I don't see how it keeps people in an imperative mindset. It allows
them to express that the sequence which is the result of the "select"
is all they need for the next part of their query.
>
LINQ isn't actually set-oriented - it's sequence-oriented, a
pipeline. It may be convenient to think in terms of sets when
writing LINQ to SQL, but LINQ to Objects should definitely be
considered in terms of sequences.
statement. If you write your query as imperative as possible, you WILL
probably have a slow query. A query written to be executed on a DB
should be written with set operations in mind, otherwise you will end
up sooner or later at the wrong end of the DBA-developer conversation
where you need to optimize the query to make it performing properly but
you don't know how.
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Comment
-
Jon Skeet [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
Frans Bouma [C# MVP] <perseus.usenet NOSPAM@xs4all.n lwrote:Yes. And a query which is executed in-process will end up as a>LINQ isn't actually set-oriented - it's sequence-oriented, a
pipeline. It may be convenient to think in terms of sets when
writing LINQ to SQL, but LINQ to Objects should definitely be
considered in terms of sequences.
A query which is executed on a db, will end up as a set-oriented
statement.
sequence-oriented statement.
It depends on what you mean by "as imperative as possible". I don't seeIf you write your query as imperative as possible, you WILL
probably have a slow query. A query written to be executed on a DB
should be written with set operations in mind, otherwise you will end
up sooner or later at the wrong end of the DBA-developer conversation
where you need to optimize the query to make it performing properly but
you don't know how.
"let" as being particularly imperative - you could regard it as
assigning an alias. The test queries I've done against LINQ to SQL
(which aren't many, admittedly) look fine to me.
--
Jon Skeet - <skeet@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Comment
-
Jon Skeet [C# MVP]
Re: Linq "into" ; and "let&qu ot; equally???
On Nov 26, 8:33 am, "Frans Bouma [C# MVP]"
<perseus.usenet NOS...@xs4all.n lwrote:No, "let" doesn't result in SelectMany - it results in another Select.>It depends on what you mean by "as imperative as possible". I don't
see "let" as being particularly imperative - you could regard it as
assigning an alias. The test queries I've done against LINQ to SQL
(which aren't many, admittedly) look fine to me.
Example: (I used 'from' here to make it more clear, I also could have
used let, it would result in a SelectMany as well)
From the spec:
"A query expression with a let clause
from x in e
let y = f
....
is translated into
from * in ( e ) . Select ( x =new { x , y = f } )"
Does that change your opinion of it?
Jon
Comment
Comment