EF核心分组并选择

问题描述 投票:0回答:1

enter image description here

帮助我处理此请求。

UseNetTopologySuite

 var query = await db.tbOrders
                         .AsNoTracking()
                         .Include(x => x.DrugStore)
                         .Include(x => x.OrderDetails)
                         .Include(x => x.Address)
                         .Include(x => x.Address.District)
                         .Include(x => x.OrderStatus)
                         .Include(x => x.Payment)
                         .Where(x => x.OrderStatusId == 1)    
                         .GroupBy(p => new
                         {
                             Id = p.DrugStoreId,
                             DrugStoreName = p.DrugStore.Name,
                             Longitude = p.DrugStore.Location.X,
                             Latitude = p.DrugStore.Location.Y,
                             Distance = p.DrugStore.Location.Distance(cur)
                         })
                         .Select(x => new viDeliveryDrugstore
                         {
                             Id = x.Key.Id,
                             DrugStoreName = x.Key.DrugStoreName,
                             Longitude = x.Key.Longitude,
                             Latitude = x.Key.Latitude,
                             Distance = x.Key.Distance,
                             Orders = x.Select(z => new viDeliveryOrder
                             {
                                 OrderId = z.Id,
                                 CreateDate = z.CreateDate,
                                 PaymentId = z.PaymentId,
                                 PaymentName = z.Payment.Name,
                                 Phone = z.Phone,
                                 Summa = z.Summa,
                                 ItemQty = z.ItemQty,
                                 DeliveryTime = z.DeliveryTime,
                                 OrderStatusId = z.OrderStatusId,
                                 DistrictName = z.Address.District.Name,
                                 Street = z.Address.Street,
                                 House = z.Address.House,
                                 Flat = z.Address.Flat,
                                 Waymark = z.Address.Waymark,
                                 Longitude = z.Address.Location.X,
                                 Latitude = z.Address.Location.Y,
                                 Items = z.OrderDetails.Select(t => new viOrderDetails
                                 {
                                     Id = t.Id,
                                     ProductId = t.ProductId,
                                     DrugName = t.Product.Drug.Name,
                                     Qty = t.Qty,
                                     Price = t.Price,
                                     TotalSum = t.TotalSum

                                 }).ToList()
                             }).ToList()
                         })
                         .OrderBy(x => x.Distance)
                         .ToListAsync();

错误消息

2020-05-10 01:51:37.8038|0|ERROR|OrderDelivery.Program|Something went wrong: System.InvalidOperationException: The LINQ expression '(GroupByShaperExpression:
KeySelector: new { 
    Id = (t.drug_store_id), 
    DrugStoreName = (s.name), 
    Longitude = (ST_X((s.location))), 
    Latitude = (ST_Y((s.location))), 
    Distance = (ST_Distance((s.location), (@__cur_0)))
 }, 
ElementSelector:(EntityShaperExpression: 
    EntityType: tbOrderHeader
    ValueBufferExpression: 
        (ProjectionBindingExpression: EmptyProjectionMember)
    IsNullable: False
)
)
    .AsEnumerable()' 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(). 
    See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

在Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)在Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCall)在System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor访问者)在System.Linq.Expressions.ExpressionVisitor.Visit(Expression节点)在Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)在Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCall)在System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor访问者)在System.Linq.Expressions.ExpressionVisitor.Visit(Expression节点)在Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

以这种形式,请求有效

       var list = await db.tbOrders
                             .AsNoTracking()
                             .Include(x => x.DrugStore)
                             .Include(x => x.OrderDetails)
                             .Include(x => x.Address)
                             .Include(x => x.Address.District)
                             .Include(x => x.OrderStatus)
                             .Include(x => x.Payment)
                             .Where(x => x.OrderStatusId == 1)    
                             .GroupBy(p => new
                             {
                                 Id = p.DrugStoreId,
                                 DrugStoreName = p.DrugStore.Name,
                                 Longitude = p.DrugStore.Location.X,
                                 Latitude = p.DrugStore.Location.Y,
                                 Distance = p.DrugStore.Location.Distance(cur)
                             })
                             .Select(x => new 
                             {
                                 Id = x.Key.Id,
                                 DrugStoreName = x.Key.DrugStoreName,
                                 Longitude = x.Key.Longitude,
                                 Latitude = x.Key.Latitude,
                                 Distance = x.Key.Distance,

                             })
                             .OrderBy(x => x.Distance)
                             .ToListAsync();

生成sql

SELECT t.drug_store_id AS "Id", s.name AS "DrugStoreName", ST_X(s.location) AS "Longitude", ST_Y(s.location) AS "Latitude", ST_Distance(s.location, @__cur_0) AS "Distance"
FROM tb_orders AS t
INNER JOIN sp_drug_stores AS s ON t.drug_store_id = s.id
WHERE t.order_status_id = 1
GROUP BY t.drug_store_id, s.name, ST_X(s.location), ST_Y(s.location), ST_Distance(s.location, @__cur_0)
ORDER BY ST_Distance(s.location, @__cur_0) 
2020-05-12 05:43:05.1453|A data reader was disposed. 
postgresql entity-framework .net-core postgis
1个回答
0
投票

sql构造可能在那里工作

select 
 group_keys,
 aggregation
from table 
GROUP BY group_keys

[Entity Frameworkожидаетфункциюагрегирования

© www.soinside.com 2019 - 2024. All rights reserved.