IEnumerable vs IQueryable Exceptions
This article won’t go into what these interfaces are, or the differences between them. For that, read this article for a good introduction.
Instead, I’ll show you an example of an Exception
you can get from IQueryable
but not from IEnumerable
, for the exact same query.
Calling Skip(-x)
Have a look at this code:
IEnumerable<int> ids = GetIds();
ids.Skip(-4);
This works fine.
The Exception
However, the following will throw an Exception
:
IQueryable<int> ids = GetIds();
ids.Skip(-4);
The Error Message
The offset specified in a OFFSET clause may not be negative.
Why?
The reason for the error is because the IQueryable<int>
creates the following SQL query:
SELECT [p].[Id]
FROM [Ids] AS [p]
ORDER BY (SELECT 1)
OFFSET @__p_0 ROWS
In SQL, offset
can’t have a negative value.