I'm trying to remove duplicates from my database. I'm using Entity Framework Core and .NET 5. EF Core is having trouble materializing my group by:
protected async Task RemoveDuplicates(CryptoInfoContext cryptoContext)
{
try
{
var duplicates = cryptoContext.HistoricalCandles
.GroupBy(x => new { x.StartDate, x.GranularitySeconds })
.Where(x => x.Count() > 1)
.ToList()
.Select(x => x.FirstOrDefault())
.ToList();
cryptoContext.RemoveRange(duplicates);
await cryptoContext.SaveChangesAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
I'm getting an error:
Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side
I don't feel like materializing all of my rows to remove the duplicates. Is there a list of known issues with group by? how can I work around this issue?