A
Linq "into" and "let" equally???
Jon said: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 <expression> into <var> ? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
Alexander said:Why painful?
"Frans Bouma [C# MVP]" <[email protected]>
???????/???????? ? ???????? ?????????:
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 <expression> into <var> ? Or am I
the only one who finds the painful mix between set oriented
functionality and imperative programming within linq a bit 'odd' ?
Frans Bouma said: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.
Jon said: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.
Frans Bouma said: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.
Jon said: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)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.