From 24aa4c51a4d310ea53b681b536db5eec4f642a3f Mon Sep 17 00:00:00 2001 From: "soroush.asadi" Date: Tue, 2 Jun 2026 23:04:53 +0330 Subject: [PATCH] fix(identity): plan-statistics LINQ translation (aggregate in memory) EF Core can't translate a conditional Count(predicate) inside a grouped Select; fetch flat rows then group/aggregate in memory. Co-Authored-By: Claude Opus 4.8 --- .../Application/Services/AdminService.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/services/identity/FlatRender.IdentitySvc/Application/Services/AdminService.cs b/services/identity/FlatRender.IdentitySvc/Application/Services/AdminService.cs index f9ff05f..fe9373b 100644 --- a/services/identity/FlatRender.IdentitySvc/Application/Services/AdminService.cs +++ b/services/identity/FlatRender.IdentitySvc/Application/Services/AdminService.cs @@ -62,16 +62,20 @@ public class AdminService(IdentityDbContext db) public async Task> GetPlanStatisticsAsync(Guid tenantId) { var now = DateTime.UtcNow; - return await db.UserPlans + // Pull flat rows then aggregate in memory — EF can't translate a conditional + // Count(predicate) inside a grouped Select. + var rows = await db.UserPlans .Where(p => p.TenantId == tenantId) - .GroupBy(p => p.PlanName) + .Select(p => new { p.PlanName, p.ExpiresAt, p.CancelledAt, p.PriceMinorPaid }) + .ToListAsync(); + return rows.GroupBy(r => r.PlanName) .Select(g => new PlanStatRow( g.Key, g.Count(), g.Count(x => x.ExpiresAt > now && x.CancelledAt == null), g.Sum(x => x.PriceMinorPaid))) .OrderByDescending(r => r.RevenueMinor) - .ToListAsync(); + .ToList(); } // ── CRM notes / tags ────────────────────────────────────────────────────