fix(guild): single-pass bucket filter in GuildRepository.SearchAsync

Replace double-correlated COUNT subqueries in the member-bucket switch
with a project-filter-project pattern that computes the count once per
row.  Add SearchAsync_bucket_2_matches_guilds_with_11_to_25_members to
cover the previously untested medium-bucket range.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-27 11:21:32 -04:00
parent 3fdc20d21f
commit 45947053c8
2 changed files with 34 additions and 9 deletions

View File

@@ -53,18 +53,18 @@ public sealed class GuildRepository : IGuildRepository
if (joinCondition != 0)
q = q.Where(g => (int)g.JoinCondition == joinCondition);
if (memberBucket != 0)
if (memberBucket is 1 or 2 or 3)
{
q = memberBucket switch
var (lo, hi) = memberBucket switch
{
1 => q.Where(g => _db.GuildMembers.Count(m => m.GuildId == g.GuildId) >= 1
&& _db.GuildMembers.Count(m => m.GuildId == g.GuildId) <= 10),
2 => q.Where(g => _db.GuildMembers.Count(m => m.GuildId == g.GuildId) >= 11
&& _db.GuildMembers.Count(m => m.GuildId == g.GuildId) <= 25),
3 => q.Where(g => _db.GuildMembers.Count(m => m.GuildId == g.GuildId) >= 26
&& _db.GuildMembers.Count(m => m.GuildId == g.GuildId) <= maxMemberCap),
_ => q,
1 => (1, 10),
2 => (11, 25),
_ => (26, maxMemberCap),
};
q = q
.Select(g => new { Guild = g, MemberCount = _db.GuildMembers.Count(m => m.GuildId == g.GuildId) })
.Where(x => x.MemberCount >= lo && x.MemberCount <= hi)
.Select(x => x.Guild);
}
return await q.Take(resultCap).ToListAsync(ct);