{"record":{"id":"5df701b4737967d0","repo":"dotnet/efcore","slug":"cosmos-sql-does-not-allow-offset-without-limit-co","errorCode":null,"errorMessage":"Cosmos SQL does not allow Offset without Limit. Consider specifying a 'Take' operation on the query.","messagePattern":"Cosmos SQL does not allow Offset without Limit\\. Consider specifying a 'Take' operation on the query\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs","lineNumber":440,"sourceCode":"            if (selectExpression.Offset != null)\n            {\n                Visit(selectExpression.Offset);\n            }\n            else\n            {\n                _sqlBuilder.Append('0');\n            }\n\n            _sqlBuilder.Append(\" LIMIT \");\n\n            if (selectExpression.Limit != null)\n            {\n                Visit(selectExpression.Limit);\n            }\n            else\n            {\n                // TODO: See Issue#18923\n                throw new InvalidOperationException(CosmosStrings.OffsetRequiresLimit);\n            }\n        }\n\n        return selectExpression;\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual Expression VisitFromSql(FromSqlExpression fromSqlExpression)\n    {\n        var sql = fromSqlExpression.Sql;\n\n        string[] substitutions;\n","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs#L422-L458","documentation":"Cosmos SQL syntax requires that any OFFSET clause be paired with a LIMIT clause. The CosmosQuerySqlGenerator emits OFFSET and then a LIMIT token, and when a SelectExpression carries an Offset but no Limit it throws InvalidOperationException at runtime during SQL generation. This is a hard restriction of the Cosmos DB SQL API surface, not an EF choice.","triggerScenarios":"A LINQ query uses .Skip(n) (which becomes Offset) without a following .Take(m) (which becomes Limit), and that query is executed against the Cosmos provider. EF cannot infer a safe default upper bound, so it refuses to emit OFFSET-only SQL.","commonSituations":"Pagination code that only forwards a page offset, hand-rolled paging that splits Skip/Take across conditional branches, or third-party paging helpers that omit Take when page size is null.","solutions":["Add a .Take(pageSize) immediately after every .Skip(skip) in the query chain.","Cap the skip with a sane Take (e.g. int.MaxValue) if you genuinely want all remaining rows after the offset.","If paging is optional, ensure Take is only omitted when Skip is also omitted."],"exampleFix":"// before\nvar page = db.Users.Skip(20).ToList();\n// after\nvar page = db.Users.Skip(20).Take(10).ToList();","handlingStrategy":"validation","validationCode":"// before executing, assert every Skip is paired with Take\nstatic IQueryable<T> Paged<T>(IQueryable<T> q, int skip, int? take)\n{\n    var withSkip = q.Skip(skip);\n    return take is { } t ? withSkip.Take(t) : withSkip.Take(int.MaxValue);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Wrap paging in a helper that always emits Take alongside Skip.","Add a unit test that asserts every paging overload produces a Take call.","Reject null/zero page size at the API boundary rather than dropping Take."],"tags":["cosmos","query-translation","pagination","sql-generation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}