liveBook · Manning

Take

As explained in Building Web APIs with ASP.NET Core

The Take method is a LINQ sequence operation that returns a specified number of elements from the beginning of a sequence.

Take returns a specified number of elements from the beginning of a sequence. In a paged board-game endpoint, it limits the query to the requested page size after Skip calculates the page offset. section 5.4.1

Listing 5.5 BoardGameController.Get() method (with paging)
[HttpGet(Name = "GetBoardGames")]
[ResponseCache(Location = ResponseCacheLocation.Any, Duration = 60)]
public async Task<RestDTO<BoardGame[]>> Get(
    int pageIndex = 0,
int pageSize = 10)
{
    var query = _context.BoardGames
.Skip(pageIndex * pageSize)
.Take(pageSize);
 
    return new RestDTO<BoardGame[]>()
    {
        Data = await query.ToArrayAsync(),
PageIndex = pageIndex,
PageSize = pageSize,
RecordCount = await _context.BoardGames.CountAsync(),
        Links = new List<LinkDTO> {
            new LinkDTO(
                Url.Action(
                    null,
                    "BoardGames",
new { pageIndex, pageSize },
                    Request.Scheme)!,
                "self",
                "GET"),
        }
    };
}

The controller applies Take(pageSize) after Skip(pageIndex * pageSize), then materializes the selected records asynchronously. section 5.4.1

Paging board games

With pageIndex = 0 and pageSize = 10, Skip removes zero records and Take returns the first ten. Default parameter values keep the endpoint usable when clients omit paging inputs. section 5.4.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains its role with Skip, zero-based page calculations, defaults, DTO metadata, and a paging test.
Examples
Uses pageIndex = 0, pageSize = 10, and pages containing five board-game records.
What it assumes
Assumes familiarity with EF Core queries, HTTP endpoints, and pageIndex/pageSize parameters.
What you can do after
Operate/configure a paged endpoint that returns only the requested number of records.
How it connects
Connects Take with Skip, paging, EF Core, zero-based indexing, and REST DTO metadata.

Also explained in

Compare how 4 books explain take →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.