Skip to content

Commit 2e43a76

Browse files
fix: Postgres EFCore query implementation
1 parent b87bf54 commit 2e43a76

3 files changed

Lines changed: 54 additions & 54 deletions

File tree

benchmark/MysqlEFCoreImpl/Queries.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,26 @@ public async Task<List<GetCustomerOrdersRow>> GetCustomerOrders(GetCustomerOrder
5151
productsQuery = productsQuery.AsNoTracking();
5252
}
5353

54-
var query = (from o in ordersQuery
55-
join i in orderItemsQuery on o.OrderId equals i.OrderId
56-
join p in productsQuery on i.ProductId equals p.ProductId
57-
where o.CustomerId == args.CustomerId
58-
orderby o.OrderedAt descending
59-
select new GetCustomerOrdersRow(
60-
o.OrderId,
61-
o.OrderedAt,
62-
o.OrderState,
63-
o.TotalAmount,
64-
i.OrderItemId,
65-
i.Quantity,
66-
i.UnitPrice,
67-
p.ProductId,
68-
p.Name,
69-
p.Category
70-
))
71-
.Skip(args.Offset)
72-
.Take(args.Limit);
73-
74-
var results = await query.ToListAsync();
75-
54+
var results = await (from o in ordersQuery
55+
join i in orderItemsQuery on o.OrderId equals i.OrderId
56+
join p in productsQuery on i.ProductId equals p.ProductId
57+
where o.CustomerId == args.CustomerId
58+
orderby o.OrderedAt descending
59+
select new GetCustomerOrdersRow(
60+
o.OrderId,
61+
o.OrderedAt,
62+
o.OrderState,
63+
o.TotalAmount,
64+
i.OrderItemId,
65+
i.Quantity,
66+
i.UnitPrice,
67+
p.ProductId,
68+
p.Name,
69+
p.Category
70+
))
71+
.Skip(args.Offset)
72+
.Take(args.Limit)
73+
.ToListAsync();
7674
return results;
7775
}
7876

benchmark/PostgresqlEFCoreImpl/Queries.cs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -28,40 +28,44 @@ string ProductCategory
2828

2929
public record GetCustomerOrdersArgs(int CustomerId, int Offset, int Limit);
3030

31+
/// <summary>
32+
/// Get customer orders with all order items and product details, ordered by date descending with pagination
33+
/// </summary>
3134
public async Task<List<GetCustomerOrdersRow>> GetCustomerOrders(GetCustomerOrdersArgs args)
3235
{
33-
var query = _dbContext.Orders
34-
.Where(o => o.CustomerId == args.CustomerId)
35-
.OrderByDescending(o => o.OrderedAt)
36-
.Skip(args.Offset)
37-
.Take(args.Limit);
36+
var ordersQuery = _dbContext.Orders.AsQueryable();
37+
var orderItemsQuery = _dbContext.OrderItems.AsQueryable();
38+
var productsQuery = _dbContext.Products.AsQueryable();
3839

3940
if (!_useTracking)
40-
query = query.AsNoTracking();
41-
42-
var results = await query
43-
.SelectMany(o => o.OrderItems.Select(i => new
44-
{
45-
Order = o,
46-
OrderItem = i,
47-
Product = i.Product,
48-
}))
49-
.Select(x => new GetCustomerOrdersRow(
50-
x.Order.OrderId,
51-
x.Order.OrderedAt,
52-
x.Order.OrderState,
53-
x.Order.TotalAmount,
54-
x.OrderItem.OrderItemId,
55-
x.OrderItem.Quantity,
56-
x.OrderItem.UnitPrice,
57-
x.Product.ProductId,
58-
x.Product.Name,
59-
x.Product.Category
60-
))
61-
.ToListAsync();
41+
{
42+
ordersQuery = ordersQuery.AsNoTracking();
43+
orderItemsQuery = orderItemsQuery.AsNoTracking();
44+
productsQuery = productsQuery.AsNoTracking();
45+
}
46+
47+
var results = await (from o in ordersQuery
48+
join i in orderItemsQuery on o.OrderId equals i.OrderId
49+
join p in productsQuery on i.ProductId equals p.ProductId
50+
where o.CustomerId == args.CustomerId
51+
orderby o.OrderedAt descending
52+
select new GetCustomerOrdersRow(
53+
o.OrderId,
54+
o.OrderedAt,
55+
o.OrderState,
56+
o.TotalAmount,
57+
i.OrderItemId,
58+
i.Quantity,
59+
i.UnitPrice,
60+
p.ProductId,
61+
p.Name,
62+
p.Category
63+
))
64+
.Skip(args.Offset)
65+
.Take(args.Limit)
66+
.ToListAsync();
6267
return results;
6368
}
64-
6569
public record AddProductsArgs(string Name, string Category, decimal UnitPrice, int StockQuantity, string? Description);
6670

6771
public async Task AddProducts(List<AddProductsArgs> args)

benchmark/SqliteEFCoreImpl/Queries.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public record GetCustomerOrdersArgs(int CustomerId, int Offset, int Limit);
4040
/// </summary>
4141
public async Task<List<GetCustomerOrdersRow>> GetCustomerOrders(GetCustomerOrdersArgs args)
4242
{
43-
// Use explicit joins instead of navigation properties to avoid loading issues
4443
var ordersQuery = _dbContext.Orders.AsQueryable();
4544
var orderItemsQuery = _dbContext.OrderItems.AsQueryable();
4645
var productsQuery = _dbContext.Products.AsQueryable();
@@ -71,8 +70,7 @@ orderby o.OrderedAt descending
7170
))
7271
.Skip(args.Offset)
7372
.Take(args.Limit)
74-
.ToListAsync();
75-
73+
.ToListAsync();
7674
return results;
7775
}
7876

0 commit comments

Comments
 (0)