gpt4 book ai didi

c# - GroupBy 无法翻译

转载 作者:行者123 更新时间:2023-12-04 13:28:06 25 4
gpt4 key购买 nike

我正在尝试从我的数据库中删除重复项。我正在使用 Entity Framework Core 和 .NET 5。EF Core 无法通过以下方式实现我的组:

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);
}
}
我收到一个错误:

Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side


我不想具体化我的所有行来删除重复项。
是否有 group by 的已知问题列表?
我该如何解决这个问题?

最佳答案

感谢@GertArnold 为我指明了正确的方向。显然 EF Group by 仅支持聚合查询。
正如本文所指出的article :

var ctx = new EntertainmentDbContext(conString);

var dataTask = ctx
.Ratings
.GroupBy(x => x.Source)
.Select(x => new {Source = x.Key, Count = x.Count()})
.OrderByDescending(x => x.Count)
.ToListAsync();

var data = await dataTask;
它将像这样生成 SQL:
SELECT "r"."Source", COUNT(*) AS "Count"
FROM "Ratings" AS "r"
GROUP BY "r"."Source"
ORDER BY COUNT(*) DESC
注:目前几乎没有其他工作,您甚至不能在 group by 之前应用 where。
另外还有一个开放的 EF Core issue请投票,以便他们真正解决此问题,而不是将其推到另一个版本

关于c# - GroupBy 无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66863901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com