Summary#
Take(m) limits a query to the first m rows; Skip(n) skips the first n. Together they page a result set
(Skip(n).Take(m)). The count for either can be a compile-time constant (Take(20)) or a runtime
integer — a variable, a parameter, or any int expression (Take(pageSize)) — exactly like the parameterized
LIMIT/OFFSET a database query uses. Order the query first (OrderBy) so paging is deterministic.
Signature#
set.Where(p).OrderBy(k).Skip(<int>).Take(<int>).ToList()
// └ OFFSET └ LIMIT (each count: a constant OR a runtime int)Description#
Take(m)returns at most the firstmrows (SQLLIMIT).Skip(n)discards the firstnrows (SQLOFFSET).Skip(n).Take(m)is the page-(n/m) window.- The count may be runtime.
Take(pageSize)/Skip(page * pageSize)accept an int variable, parameter, or expression — not just a literal. The value is read when the query runs (like a bound SQL parameter), so a page size the caller chose at runtime just works. - The count must be an integer. A non-integer argument is a compile error.
- Take never throws and never over-returns. Asking for more rows than exist returns all of them;
Take(0)returns none — matching C#. - Order first for determinism. Without an
OrderBy, the database may return any rows; page a sorted query. - What it composes with.
Where,OrderBy/ThenBy,Include,Distinct, and the single-row terminals.Skipmay not precede aSelectprojection —Takemay, andSkipmay not (page first and project after, or project into aclassand page the result). Neither may precede aGroupBy: onlyWheremay.
Examples#
entity Order {
[Required] string Name;
decimal Total;
}
// The page and size are PARAMETERS — chosen by the caller at runtime.
Order[] PageOrders(int page, int size) {
return Order
.OrderByDescending(o => o.Total)
.Skip(page * size)
.Take(size)
.ToList();
}
// A constant count still works exactly as before.
Order[] TopFive() {
return Order.OrderByDescending(o => o.Total).Take(5).ToList();
}
// Take clamps: n larger than the row count returns all rows; 0 returns none.
int HowMany(int n) {
var rows = Order.OrderBy(o => o.Name).Take(n).ToList();
return rows.Count;
}See also#
- Union / Concat / Intersect / Except — Union / Concat / Intersect / Except over paged query sets
- List OrderBy (in-memory) — OrderBy / Take on an in-memory
List<T>(not a database set)