My linq query fails when I try to execute it on db side:
public async Task<List<MatchEntity>> GetMatchesAsync(List<string> teammatesChampionNames, List<string> enemiesChampionNames)
{
return await _context.Matches.Include(x => x.Teams).ThenInclude(x => x.Participants).AsSplitQuery()
.Where(x =>
teammatesChampionNames.All(y => x.Teams.Any(z => z.Participants.Any(i => i.ChampionName == y)))
&& enemiesChampionNames.All(y => x.Teams.Any(z => z.Participants.Any(i => i.ChampionName == y)))).ToListAsync();
}
Do you know whether it is possible to rewrite it? I am using .EF Core 7 and SQL Server
1条答案
按热度按时间9rygscc11#
For the first condition (
teammatesChampionNames.All...
), the idea is that if allChampionName
s of allParticiants
of allTeams
are collected, you want theMatches
where all of these names occur inteammatesChampionNames
. This is true ifteammatesChampionNames
contains these names and when there are as many of these occurring names as there are inteammatesChampionNames
.Applying the same reasoning to the second condition, the query can be rewritten in this form that EF can translate: