Nice write-up. Which optimization gave you the biggest performance boost in a real project?
🚀 EF Core Query Optimization for Beginners
2 Comments
@[peponder]
Thanks for the great comment!
This example actually came from a real optimization I made in one of our applications.
Initially, my query looked something like this:
var offers = await db.Offers
.Include(o => o.Customer)
.Include(o => o.OfferLines)
.ThenInclude(l => l.Product)
.Where(o => o.DeliveryDate >= date &&
o.DeliveryDate < nextDay &&
(o.IsCompleted == null || o.IsCompleted == 0))
.OrderBy(o => o.Id)
.ToListAsync(cancellationToken);
I then mapped the entities to DTOs after the query had executed.
The application was noticeably slow, especially as the number of offers and lines increased. To understand why, I started looking at the SQL that EF Core was generating. That was the turning point.
I realized I didn't actually need full Offer, Customer, OfferLine, and Product entities—I only needed a handful of properties for the UI.
So I replaced the Include() calls with a direct projection into DTOs:
var offerDtos = await db.Offers
.AsNoTracking()
.Where(o => o.DeliveryDate >= date &&
o.DeliveryDate < nextDay &&
(o.IsCompleted == null || o.IsCompleted == 0))
.OrderBy(o => o.Id)
.Select(o => new OfferForWarehouseDto
{
OfferId = o.Id,
CustomerName = o.Customer.Name,
DeliveryDate = o.DeliveryDate,
OfferDate = o.OfferDate,
Status = o.Status,
Comments = o.Comments,
WarehouseStatus = "Pending",
Lines = o.OfferLines
.Select(l => new WarehouseLineDto
{
OfferLineId = l.Id,
ItemCode = l.ItemCode,
ItemDescription = l.ItemDescription,
Color = l.Color,
Unit = l.Unit,
OrderedQuantity = l.Quantity,
CurrentStock = l.Product.Stock
})
.ToList()
})
.ToListAsync(cancellationToken);
The difference was significant. The generated SQL became much leaner, EF Core only selected the columns I actually needed, memory usage dropped, and the page loaded much faster.
One lesson I learned is that if you're trying to optimize EF Core, don't just look at your LINQ look at the SQL it generates. Seeing the actual query often makes it obvious where the bottlenecks are.
This is around the experience that really changed how I write EF Core queries. Now, whenever performance matters, I inspect the generated SQL first instead of assuming the LINQ tells the whole story.
Please log in to add a comment.
Please log in to comment on this post.
More Posts
- © 2026 Coder Legion
- Feedback / Bug
- Privacy
- About Us
- Contacts
- Premium Subscription
- Terms of Service
- Early Builders
https://www.linkedin.com/in/spyros-ponaris-913a6937/ Show less
More From Spyros
Related Jobs
- Sr. Core Backend EngineerRoute · Full time · Brazil
- Software Engineer, Test & Infrastructure II (Bilingual Spanish)Vail Systems · Full time · Springfield, IL
- Machine Learning Engineer, Runtime & OptimizationWaymo · Full time · Mountain View, CA
Commenters (This Week)
Contribute meaningful comments to climb the leaderboard and earn badges!