Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Multi Query Selects with EF Core

Multi Query Selects with EF Core
January 20, 2023 3 min read BEN ABT
.NET EF Core

This article was written in 2023. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

Often there is a situation where you need to load several pieces of information from a database or table at the same time in order to display certain content. The prime example of such a situation is paging:

  • you need the total number of entries, so that the number of pages can be calculated
  • you need the current page of entries, e.g. page 7 with 50 entries per page

With a manual select this is no problem, it’s super easy e.g. Dapper - a simple object mapper for .NET .

C#
 1// sql query - please dont use * in your prod code, this is just a sample.
 2const string query = @"
 3    SELECT * FROM Users WHERE IsActive = @isActive
 4                        ORDER BY Name
 5                        OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY;
 6
 7    SELECT COUNT(*) FROM Users WHERE IsActive = @isActive;";
 8
 9// load
10var sqlParams = new { isActive = true, skip = 10, take = 5 };
11using var multiResult = await myConnection
12                            .QueryMultipleAsync(query, sqlParams)
13                            .ConfigureAwait(false);
14
15// read
16var users           = multi.Read<User>().ToList();
17var totalUserCount  = multi.ReadSingle<int>();
18
19// return
20return new(users, totalUserCount);

Here the query is performing two selects, the first retrieves all the users which are active and the second one retrieves the count of all users which are active. myConnection.QueryMultiple is used to execute the query, which returns multiple result sets. You can use multi.Read<T>() method to retrieve the results. multiResult.Read<User>().ToList(); retrieves all the rows from the first query and returns it as a list of ‘User’ objects. multiResult.ReadSingle<int>(); retrieves the single integer from the second query.

Not supported in EF Core by default

The problem is: this scenario, although it is a totally simple everyday scenario, was never supported in EF Core and is not until today (January 2023). For years people ask for this feature, for years people ask for it - but for years there is also the answer: it is on the roadmap: Execute multiple LINQ queries in a single round-trip (aka Expose batching read API to users) And although this scenario should affect almost every application, in my eyes, features that don’t affect so many applications and should be worlds less important in terms of priority have been coming lately.

The Community

Thanks to the community - and especially the developers behind ZZZ Projects - we have the possibility to run several queries in one round trip:

C#
 1IOrderedQueryable<UserEntity> queryUsers = myDbContext.Users.OrderBy(u => u.Name);
 2
 3// queries
 4QueryFutureEnumerable<UserEntity> usersQuery = queryUsers.Skip(skip).Take(take)
 5                            .Future(); // take care of this!
 6
 7QueryFutureValue<int> totalUsersCountQuery = queryUsers
 8                            .DeferredCount().FutureValue(); // take care of this!
 9
10// execute query
11List<UserEntity> users = await usersQuery.ToListAsync(ct) // this executes both queries!
12                                    .ConfigureAwaitFalse();
13
14int totalUserCount =     await totalUsersCountQuery.ValueAsync(ct)
15                                    .ConfigureAwaitFalse();
16
17// return
18return new(users, totalUserCount);

This Query Future is an extension for Entity Framework Core (EF Core) that provides additional features for querying data in a database. One of the main features of EF Core Plus Query Future is the ability to perform multiple queries asynchronously and combine the results into a single queryable object. The Future() and FutureValue() (and more!) methods are available in the Z.EntityFramework.Plus.EFCore NuGet package.

Related articles

Advanced Usage of MSSQL Indexed Views in EF Core

Jan 13, 2025 - 5 min read

Advanced Usage of MSSQL Indexed Views in EF Core

In the world of Microsoft SQL Server , views are a powerful tool for simplifying complex data queries. Simple views offer no real …

Increase Command Timeout for EF Core Migration Bundles

Jan 3, 2025 - 2 min read

Increase Command Timeout for EF Core Migration Bundles

EF Core Migration Bundles are standalone executable files that contain one or more Entity Framework Core migrations and can be applied …

New Data Annotation attributes with .NET 8

Nov 19, 2023 - 2 min read

New Data Annotation attributes with .NET 8

With .NET 8, the System.ComponentModel.DataAnnotations Namespace has been revised and some new attributes have been added that have been …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact