Querying with LINQ to Entities
LINQ to Entities
The primary way to query data in EF Core is to write LINQLINQ: query syntax and method syntaxOne SQL-like way to query many kinds of data, written in two equivalent forms.Opens in a new tab queries against the DbSet<TEntity> properties of your DbContext. EF Core translates those LINQ queries into database-specific SQL queries, or other query languages for non-relational providers, and they are executed against the database.
Line 3 starts from a DbSet. Line 4 is the filter that becomes part of the SQL. ToListAsync() on line 5 is what runs the query (see deferred executionDeferred executionA LINQ query does not run when you write it. It runs when you enumerate it.Opens in a new tab).
public async Task<List<Blog>> GetBlogsContainingDotNetAsync(BloggingContext context){ var blogs = await context.Blogs .Where(b => b.Url.Contains("dotnet")) .ToListAsync(); return blogs;}SQL generation
The database provider is responsible for translating the LINQ expression tree into efficient SQL queries that the target database understands. Examples of providers are Microsoft.EntityFrameworkCore.SqlServer for SQL Server and Npgsql.EntityFrameworkCore.PostgreSQL for PostgreSQL. This is the IQueryable<T> side of LINQ providersLINQ providers: IQueryable and IEnumerableWhere a LINQ query actually runs depends on its provider, and on IQueryable versus IEnumerable.Opens in a new tab.
Sources
- Querying Data, EF Core, Microsoft Learn · accessed May 16, 2025