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.
相反,我将向您展示一个Exception
的示例,您可以从IQueryable
而不是IEnumerable
那里获得完全相同的查询。
呼叫跳过(-x) (Calling Skip(-x))
Have a look at this code:
看一下这段代码:
IEnumerable<int> ids = GetIds();
IEnumerable<int> ids = GetIds();
ids.Skip(-4);
ids.Skip(-4);
This works fine.
这很好。
例外 (The Exception)
However, the following will throw an Exception
:
但是,以下内容将引发Exception
:
IQueryable<int> ids = GetIds();
IQueryable<int> ids = GetIds();
ids.Skip(-4);
ids.Skip(-4);
错误讯息 (The Error Message)
The offset specified in a OFFSET clause may not be negative.
OFFSET子句中指定的偏移量不能为负。
为什么? (Why?)
The reason for the error is because the IQueryable<int>
creates the following SQL query:
错误的原因是因为IQueryable<int>
创建以下SQL查询:
SELECT [p].[Id]
FROM [Ids] AS [p]
ORDER BY (SELECT 1)
OFFSET @__p_0 ROWS
In SQL, offset
can’t have a negative value.
在SQL中, offset
不能为负值。
翻译自: https://medium.com/swlh/ienumerable-vs-iqueryable-exceptions-daa09ce4d1a6