在Entity Framework Core中执行GroupJoin和Join in单个查询

时间:2018-10-07 14:58:22

标签: c# linq asp.net-core entity-framework-core

我正在尝试使用Linq在groupjoin内进行联接。这是场景-对于订单中的每个订单项,我想从该订单项的变式中获取一些属性(例如存储在变式和产品数据库中的变式选项和imageid)。我想加入lineitem,变体和产品数据库以获取这些有关lineitem的额外信息,然后将这些lineitem与订单表进行组联接。我已经做到了,并使用两个查询获得了理想的结果,但是我不确定运行了多少个数据库查询。在第一种情况下(即在联接中,无法指定限制,因此我只想确保仅在调用.ToListAsync()时才评估下面代码中的第一个iqueryable)。这是正确的方法吗?任何帮助将不胜感激!

  public async Task<dynamic> GetOrdersAsync(User user, int pageNumber = 1)
        {
            var perPage = 10;
            var lineItemAndVariant = from l in _context.LineItems
                                     join v in _context.Variants
                                     on l.EcomPlatformVariantId equals v.EcomPlatformVariantId
                                     join p in _context.Products
                                     on v.ProductId equals p.Id
                                     where p.UserId == user.Id
                                     select new
                                     {
                                         l.Id,
                                         l.OrderId,
                                         l.Price,
                                         l.Quantity,
                                         l.EcomPlatformLineItemId,
                                         l.EcomPlatformVariantId,
                                         l.Title,
                                         ImageId = v.ImageId ?? p.ThumbnailId, // for adding image 
                                         p.MyVendor,
                                         p.MyVendorId,
                                         v.Option1,
                                         v.Option2,
                                         v.Option3,
                                         p.VariantOption1Label,
                                         p.VariantOption2Label,
                                         p.VariantOption3Label
                                     };


            var orders = await _context.Orders
                .Where(o => o.UserId == user.Id)
                .OrderByDescending(o => o.Id)
                .Skip(perPage * (pageNumber - 1)).Take(perPage)
                .GroupJoin(lineItemAndVariant, o => o.Id, l => l.OrderId,
                (o, lineItems) => new
                {
                    o.Id,
                    o.EcomPlatformOrderId,
                    o.CustomerEmail,
                    o.FirstName,
                    o.LastName,
                    o.Address1,
                    o.Address2,
                    o.City,
                    o.Company,
                    o.Country,
                    o.Currency,
                    o.OrderNumber,
                    o.Phone,
                    o.Province,
                    o.TotalPrice,
                    o.UserId,
                    o.Zip,
                    o.CreatedAt,
                    LineItems = lineItems
                })
                .AsNoTracking().ToListAsync();


            return orders;

        }

0 个答案:

没有答案