我试图在ef core 3.0中建立一个查询,该查询从db服务器获取完整过程 IEnumerable<int> stIds = stateIds; var rtables = await db.Order. Join(db.OrderDetail, order => order.OrderId, orderdetail => orderdetail.OrderId, (order, orderdetail) => new { order, orderdetail }). Where(x => x.order.SellerId == sellerId && stIds.Contains(x.orderdetail.OrderStateId)&&x.order.RtableId != null) .GroupBy(x => x.order.RtableId ) .Select(x => new RtableState { RtableId = x.Key ?? 0, OrderStateId = x.OrderByDescending(x => x.orderdetail.OrderStateId).Select(x => x.orderdetail.OrderStateId).FirstOrDefault() }).ToListAsync();我收到此错误:{ “消息”:“处理LINQ表达式'AsQueryable f__AnonymousType52>(OrderByDescending f__AnonymousType52,int>(\ r \ n来源:NavigationTreeExpression \ r \ n值:默认值(IGrouping, f__AnonymousType52>)\ r \ n表达式:(未处理的参数:e),\ r \ n keySelector:(x)=> x.orderdetail.OrderStateId)),由'NavigationExpandingExpressionVisitor'进行了失败,这可能表示EF Core中存在错误或限制。有关更多详细信息,请参见https://go.microsoft.com/fwlink/?linkid=2101433。”, “内部”:“” }我知道查询对于EF Core 3.0来说太复杂了,但这是一个错误还是应该解决?我的解决方案是拆分请求。IEnumerable<int> stIds = stateIds; var rtableStatesServer = await db.Order. Join(db.OrderDetail, order => order.OrderId, orderdetail => orderdetail.OrderId, (order, orderdetail) => new { order, orderdetail }). Where(x => x.order.SellerId == sellerId && stIds.Contains(x.orderdetail.OrderStateId) && x.order.RtableId != null) .GroupBy(x => new RtableState { RtableId = x.order.RtableId ?? 0, OrderStateId = x.orderdetail.OrderStateId }) .Select(x => new RtableState { RtableId = x.Key.RtableId, OrderStateId = x.Key.OrderStateId }).ToListAsync(); var rtableStates = rtableStatesServer.GroupBy(r => r.RtableId, (key, value) => new RtableState { RtableId = key, OrderStateId = value.OrderByDescending(x=>x.OrderStateId).Select(x => x.OrderStateId).FirstOrDefault() }).ToList(); (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 如异常消息中所示,问题是由表达式引起的x.OrderByDescending(y => y.orderdetail.OrderStateId) .Select(y => y.orderdetail.OrderStateId) .FirstOrDefault()其中x是IGrouping<,>运算符产生的GroupBy。 这可能表示EF Core中存在错误或限制。我认为这是一个局限性,因为GroupBy包含除键和聚合表达式以外的表达式的结果没有自然的SQL等效项,因此它可能永远无法解决。通用解决方案是在可能的情况下避免使用GroupBy并使用具有相关子查询的替代构造。但是此特定查询具有简单的自然解决方案,因为表达式set.OrderByDescending(item => item.Property).Select(item => itm.Property).FirstOfDefault()可以用set.Max(item => item.Property)这是一个标准(因此受支持的汇总)。将上述问题表达式替换为x.Max(y => y.orderdetail.OrderStateId)问题就会解决。 (adsbygoogle = window.adsbygoogle || []).push({}); 09-10 06:29