473,468 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linq "into" and "let" equally???

Linq "into" and "let" equally???

http://www.alvas.net - Audio tools for C# and VB.Net developers

Nov 23 '07 #1
11 12514
On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.netwrote:
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.Range(0, 10)
let y = x*2
select x+y;

But if you do:

var query = from x in Enumerable.Range(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
Nov 23 '07 #2
Sorry, what?
Nov 23 '07 #3
Jon Skeet [C# MVP] wrote:
On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.netwrote:
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.Range(0, 10)
let y = x*2
select x+y;

But if you do:

var query = from x in Enumerable.Range(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 <expressioninto <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#)
------------------------------------------------------------------------
Nov 23 '07 #4
On Nov 23, 9:27 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
But if you do:
var query = from x in Enumerable.Range(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 <expressioninto <var? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
It's a good indicator that the only thing you care about from the
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
Nov 23 '07 #5
Thank You! Very good example!

http://www.alvas.net - Audio tools for C# and VB.Net developers
"Jon Skeet [C# MVP]" <sk***@pobox.com???????/???????? ? ????????
?????????:
news:c0**********************************@o42g2000 hsc.googlegroups.com...
On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.netwrote:
>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.Range(0, 10)
let y = x*2
select x+y;

But if you do:

var query = from x in Enumerable.Range(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

Nov 23 '07 #6
Why painful?

http://www.alvas.net - Audio tools for C# and VB.Net developers
"Frans Bouma [C# MVP]" <pe******************@xs4all.nl???????/???????? ?
???????? ?????????: news:xn***************@news.microsoft.com...
Jon Skeet [C# MVP] wrote:
>On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.netwrote:
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.Range(0, 10)
let y = x*2
select x+y;

But if you do:

var query = from x in Enumerable.Range(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 <expressioninto <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#)
------------------------------------------------------------------------

Nov 23 '07 #7
Alexander Vasilevsky wrote:
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.

FB
>

"Frans Bouma [C# MVP]" <pe******************@xs4all.nl>
???????/???????? ? ???????? ?????????:
news:xn***************@news.microsoft.com...
Jon Skeet [C# MVP] wrote:
>On Nov 23, 8:22 am, "Alexander Vasilevsky" <al...@alvas.netwrote:
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.Range(0, 10)
let y = x*2
select x+y;
>
But if you do:
>
var query = from x in Enumerable.Range(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 <expressioninto <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#)
------------------------------------------------------------------------
Nov 24 '07 #8
Frans Bouma [C# MVP] <pe******************@xs4all.nlwrote:
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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 24 '07 #9
Jon Skeet [C# MVP] wrote:
Frans Bouma [C# MVP] <pe******************@xs4all.nlwrote:
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.

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.
A query which is executed on a db, will end up as a set-oriented
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#)
------------------------------------------------------------------------
Nov 25 '07 #10
Frans Bouma [C# MVP] <pe******************@xs4all.nlwrote:
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.
Yes. And a query which is executed in-process will end up as a
sequence-oriented 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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Nov 25 '07 #11
On Nov 26, 8:33 am, "Frans Bouma [C# MVP]"
<perseus.usenetNOS...@xs4all.nlwrote:
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)
No, "let" doesn't result in SelectMany - it results in another Select.

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
Nov 26 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
5
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them...
1
by: Big Dave | last post by:
I'm having trouble using the update command of a dataadapter. It would help if I could watch it go through each record in the datatable and see what it's doing. Is there a way to do this? Also,...
4
by: Ian Sparks | last post by:
This is probably stupid and/or misguided but supposing I'm passed a byte-string value that I want to be unicode, this is what I do. I'm sure I'm missing something very important. Short version : ...
2
by: imonline | last post by:
Hi, I am generating XML and setting it into the string field. Which will be again generated into xml. Now the last xml which has xml in string field converts into "&gt" for the xml which is set...
3
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Does anyone know why I have no "Step Into" option during debugging in VS 2005/C# app? I have "Step Over" but no "Step Into" even though it would be stepping into one of my own functions inside the...
5
by: NoWorries | last post by:
I am getting hung up on how to get a piece of data into a char array the way it is being handled, here is a few snippets in order to help show what I'm trying to do. a call is made ...
10
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it...
4
by: dominique | last post by:
Hello All, In a wx GUI, I would like to let the user choose between >, < or =. So, I created a combobox and when the user chooses ">" for instance, I wanted to return (the objective is to send...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.