liveBook · Manning

LINQ

As explained in Building Web APIs with ASP.NET Core

LINQ is a .NET language-integrated query facility that provides a uniform syntax and API for querying and transforming data from different sources.

LINQ (Language Integrated Query) gives C# a uniform way to fetch, filter, transform, sort, and aggregate data from different sources. In an EF Core API, it lets CRUD code work with C# objects and query operators instead of writing database-specific SQL for every operation. section 5.0 section 5.1

LINQ queries commonly use two equivalent styles:

  • Query syntax resembles SQL: select board games where Year equals 2000. section 5.1.1
  • Method syntax uses standard C# methods and lambda expressions, such as Where(bg => bg.Year == 2000). section 5.1.1
  • Standard operators support filtering, projection, aggregation, sorting, and other query-like operations. section 5.1.1

LINQ works over IEnumerable<T> collections and IQueryable<T> queries. With EF Core, a DbSet<T> exposes IQueryable<T>, so chained operators build an expression tree. EF Core translates that representation into database-specific SQL, and execution occurs when the query is materialized, for example with ToArray(). section 5.1.3 section 5.7

Filtering board games

Given an array containing Axis & Allies, Citadels, and Terraforming Mars, query syntax can select the game published in 2000: `var boardgames = from bg in data where bg.Year == 2000 select bg;`. Method syntax expresses the same operation as `var boardgames = data.Where(bg => bg.Year == 2000);`. section 5.1.1

var query = _context.BoardGames
   .Where(b => b.Year == 2020)
   .OrderBy(b => b.Name);

A chained EF Core query filters board games by year and orders them by name, illustrating composable method syntax. section 5.1.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Traces syntax, operators, interfaces, expression trees, SQL translation, chaining, and deferred execution.
Examples
Uses board-game arrays, year filtering, chained ordering, DbSet queries, expression trees, and ToArray() materialization.
What it assumes
Assumes basic DML knowledge while introducing LINQ syntax, method parameters, operators, and query interfaces.
What you can do after
Apply it via EF Core to implement filtering, ordering, and other CRUD queries without writing each SQL statement manually.
How it connects
Connects C#, IEnumerable<T>, IQueryable<T>, EF Core, expression trees, ORM mapping, and SQL databases.

Also explained in

Compare how 3 books explain LINQ →

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.