liveBook · Manning

Dynamic LINQ

As explained in Building Web APIs with ASP.NET Core

Dynamic LINQ is a .NET library that enables LINQ query operations to be constructed from string expressions at runtime.

Dynamic LINQ lets a .NET application construct LINQ query operations from strings at runtime. It is useful when a client must choose a sort column without a separate conditional branch for every column. section 5.4.2

Use it to add client-selected sorting to a paged query:

  • Install System.Linq.Dynamic.Core through the Package Manager console or dotnet CLI. section 5.4.2
  • Import System.Linq.Dynamic.Core so its string-based extension methods are available. section 5.4.2
  • Pass the client’s sortColumn value to OrderBy(), then apply Skip() and Take() for paging. section 5.4.2

Paged board-game query

var query = _context.BoardGames .OrderBy(sortColumn) .Skip(pageIndex * pageSize) .Take(pageSize);

Validate or restrict sortColumn before passing it to OrderBy(). Unchecked input can cause runtime errors and may create SQL-injection risk if the ORM does not prevent it. section 5.4.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains installation, importing, string-based OrderBy, paging, and security concerns, while deferring validation details.
Examples
Uses a BoardGameController query that orders records dynamically before applying Skip and Take.
What it assumes
Assumes familiarity with LINQ, lambda-based OrderBy, EF Core queries, and basic paging parameters.
What you can do after
Apply it via System.Linq.Dynamic.Core to implement runtime-selected ordering in a database-backed LINQ query.
How it connects
Connects string expressions with LINQ providers, EF Core, database ordering, paging, and data validation.

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.