【发布时间】:2020-10-26 05:24:07
【问题描述】:
在 .Net 框架中,我有这个工作查询:
IEnumerable<Cars> LatestCars = await _context.Cars.Where(x => x.IsActive == true && x.IsDeleted == false)
.GroupBy(y => y.ManufacturerId)
.Select(z =>
z.OrderByDescending(k => k.ReleaseDate)
.FirstOrDefault()
)
.OrderByDescending(l => l.ReleaseDate)
.Take(5)
.ToListAsync();
这基本上得到了不同制造商发布的最新 5 款汽车。
但是当我切换到 .NET Core 时。此查询不再起作用。运行时出现此错误:
System.InvalidOperationException: The LINQ expression '(GroupByShaperExpression:
KeySelector: (g.ManufacturerId),
ElementSelector:(EntityShaperExpression:
EntityType: Cars
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
)
)
.OrderByDescending(p => p.ReleaseDate)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
你有什么建议吗?谢谢。
【问题讨论】:
-
试试看这个:stackoverflow.com/questions/59965647/…,可能对你有帮助
-
我没看到您在 Select 中选择了什么。