querybuilder icon indicating copy to clipboard operation
querybuilder copied to clipboard

.Take(0) returns all records

Open davehauser opened this issue 3 months ago • 1 comments

From the Docs:

If you are coming from a Linq background here is a bonus for you. You can use the Skip and Take methods as aliases for Offset and Limit enjoy :)

Coming from Linq I would expect the following query to return all records for authorized = true and zero records if authorized = false.

var query = authorized
  ? new Query("Items")
  : new Query("Items).Take(0);

In fact, however, SqlKata generates the same SQL code from both queries:

SELECT
  *
FROM
  [Items]

As stated in the documentation, the Take method is merely an alias for Limit, so 0 is treated as “no limit.” Regarding Limit, this also makes sense from a linguistic point of view: zero limit = no limit. With Take, on the other hand, this is very misleading: take zero ≠ take everything

Changing the Take method to treat 0 as zero would, of course, be a fundamental change and a delicate one as well (and also a breaking change). But at the same time, it would make the API more precise and easier to understand.

davehauser avatar Oct 14 '25 15:10 davehauser

You have a point here, this is a breaking change though, so worth discussing it further, but for your use case, I would set a falsy where condition instead of limit, Limit() can be easily overriden, i.e. if you are wrapping your query with some logic to handle pagination, etc..., while this is not the case for .WhereRaw("1 = 0").

So my suggestion would be to always restrict your queries by a falsy where instead.

query.WhenNot(authorized, q => q.WhereRaw("1 = 0"))

Note: be aware of top level or conditions though, anyway it's a bad practice to have top-level or conditions

ahmad-moussawi avatar Oct 31 '25 09:10 ahmad-moussawi