Re: Are Linq-SQL methods commutative



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#)
------------------------------------------------------------------------
.



Relevant Pages

  • Re: Finally which ORM tool?
    ... you manipulate the linq query IF you're ... implement IEnumerablebut had an Execute() method which gave ... Every Linq provider will implement ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finally which ORM tool?
    ... method used INSIDE the query is passed as the value immediately, ... That's also info not NEEDED to write correct queries on the ... Sequence and set aren't equal, true, but in this case, where linq ... to use the subquery train, I have to use joins, or rely on the provider ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finally which ORM tool?
    ... of a query;), hence my question. ... IEnumerabledirectly but instead having an Execute method taking ... But it's not a problem with LINQ itself. ... Every Linq provider will implement ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finally which ORM tool?
    ... manipulate the linq query IF you're executing it at that moment. ... simply because the declaration construction was with 'CHOPS'. ... implement IEnumerablebut had an Execute() method which gave back ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The Microsoft Jet database engine does not recognize...
    ... In the first query, ... Choose Parameters on the Query menu. ... Access opens a dialog. ... (tblGeoLoc INNER JOIN (tblLocPicker INNER ...
    (microsoft.public.access.queries)