我如何结合这些Linq查询以及如何提高查询速度

时间:2018-07-13 01:50:31

标签: c# linq entity-framework-core

我有此SQL脚本,需要将其转换为与EF Core一起使用的Linq:

SELECT  
    A.*, B.*
FROM
    [Orders] AS A
JOIN
    (SELECT TOP 1 WITH TIES *
     FROM [OrderMeta]
     ORDER BY ROW_NUMBER() OVER (PARTITION BY OrderId ORDER BY PostDate DESC)
    ) AS B ON (b.OrderId = A.OrderId)

这是我将其转换为的内容:

public async Task<List<GetAllOrdersResult>> AdminGetOrderList()
        {
            using (var db = new SoulDrawContext())
            {
                var query = from A in db.Orders
                            join B in db.OrderMeta
                            on A.OrderId equals B.OrderId
                            select new GetAllOrdersResult
                            {
                                OrderId = A.OrderId,
                                UserId = A.UserId,
                                OrderDate = A.OrderDate.ToLocalTime().ToString("yyyy-MM-dd"),
                                Currency = A.Currency,
                                Amount = A.Amount,
                                PaymentMethod = A.PaymentMethod,
                                ShipTo = A.ShipTo,
                                Status = B.Status,
                                PostDate = B.PostDate.ToLocalTime().ToString("yyyy-MM-dd hh:mm"),
                                RefOrderId = A.RefOrderId
                            };
                var query1 = from a in query
                             group a by a.OrderId
                             into g
                             orderby g.FirstOrDefault().PostDate
                             select new GetAllOrdersResult
                             {
                                 OrderId = g.FirstOrDefault().OrderId,
                                 UserId = g.FirstOrDefault().UserId,
                                 OrderDate = g.FirstOrDefault().OrderDate,
                                 Currency = g.FirstOrDefault().Currency,
                                 Amount = g.FirstOrDefault().Amount,
                                 PaymentMethod = g.FirstOrDefault().PaymentMethod,
                                 ShipTo = g.FirstOrDefault().ShipTo,
                                 Status = g.FirstOrDefault().Status,
                                 PostDate = g.FirstOrDefault().PostDate,
                                 RefOrderId = g.FirstOrDefault().RefOrderId
                             };
                return await query1.ToListAsync();
            }
        }

问题:

在SSMS中运行的SQL脚本运行正常,查询大约需要32毫秒才能完成,但是EF内核的linq查询大约需要9600毫秒。

order表中大约有2,000条记录,ordermeta表中有60,000条记录。

待办事项

  1. 如何将SQL脚本正确转换为Linq? (或结合两个linq查询?)

  2. 如何提高查询速度?

1 个答案:

答案 0 :(得分:-1)

尝试这个,希望它能解决您的问题

 public async Task<List<GetAllOrdersResult>> AdminGetOrderList()
            {
                using (var db = new SoulDrawContext())
                {
                                  var query = from A in db.Orders group A by A.OrderId into g 
                                    join B in db.OrderMeta
                                    on g.FirstOrDefault().OrderId equals B.OrderId
                                    select new GetAllOrdersResult
                                    {
                                        OrderId = g.FirstOrDefault().OrderId,
                                        UserId = g.FirstOrDefault().UserId,
                                        OrderDate = g.FirstOrDefault().OrderDate.ToLocalTime().ToString("yyyy-MM-dd"),
                                        Currency = g.FirstOrDefault().Currency,
                                        Amount = g.FirstOrDefault().Amount,
                                        PaymentMethod = g.FirstOrDefault().PaymentMethod,
                                        ShipTo = g.FirstOrDefault().ShipTo,
                                        Status = B.Status,//now your B in scope so you can use Directly
                                        PostDate = B.PostDate.ToLocalTime().ToString("yyyy-MM-dd hh:mm"),
                                        RefOrderId = g.FirstOrDefault().RefOrderId
                                    };
                         query.OrderBy(B.PostDate);
                        return await query.ToListAsync();
                    }
           }