Re: Are Linq-SQL methods commutative
- From: "Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xxxxxxxxx>
- Date: Mon, 26 Nov 2007 01:04:41 -0800
Nicholas Paldino [.NET/C# MVP] wrote:
Andrus,
Absolutely different results.
Now that's unexpected. In database land, you can't get 2 different
results.
Assuming the ordering is the same on each of them (because Skip and
Take make no sense without ordering, LINQ to SQL will create an order
for you, which irritates me to no end, but that's a separate thread),
Why? SELECT * FROM Table without ordering has no defined ordering, so
using a limit + skip operator on that set results in a set of undefined
rows, you'll never know what rows will be returned.
they will produce different results.
Say your query will produce the ordered set {1, 2, 3}. Let n = 1,
m = 2.
The first query:
var query = query.Skip(n).Take(m);
Will return the ordered set {2, 3}, while the second query:
var query = query.Take(m).Skip(n);
Will return the ordered set {2}.
The reason for this is that in the first query, the Skip method
skips one element, then takes the remaining two, while in the second
query, the first two elements are taken, and then the first one is
skipped.
I tried it out to be sure, and indeed Linq to Sql generates two
different queries (which really hurt my eyes but that's another story).
The thing is though that on databases, people will use the take/skip
combination to page through a bigger resultset. However consulting the
manual for both shows no word whatsoever about the order in which these
two statements have to be specified.
I do understand the order, but it's a bit strange as well. For
example, Linq apparently has no problem with an Order by placed in
front of a where, however there IS an order in take/skip which are used
combined as a paging mechanism. Of course, this follows from the specs
of both, but semantically, the intention of what people want to do,
e.g. to page, shouldn't require an order in the statements for paging,
if other elements in the query also don't really require an order (they
do, but that's whiped under the rug)
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#)
------------------------------------------------------------------------
.
- Follow-Ups:
- Re: Are Linq-SQL methods commutative
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Are Linq-SQL methods commutative
- From: Jon Skeet [C# MVP]
- Re: Are Linq-SQL methods commutative
- References:
- Are Linq-SQL methods commutative
- From: Andrus
- Re: Are Linq-SQL methods commutative
- From: Nicholas Paldino [.NET/C# MVP]
- Are Linq-SQL methods commutative
- Prev by Date: Re: Linq "into" and "let" equally???
- Next by Date: Array.Sort( comports )
- Previous by thread: Re: Are Linq-SQL methods commutative
- Next by thread: Re: Are Linq-SQL methods commutative
- Index(es):
Relevant Pages
|