如何改善此LINQ查询?

时间:2018-09-29 18:23:56

标签: c# .net linq

我担心我在这里要进行n + 1查询,该如何改善呢?

var inventories = AppContext.Inventories
                            .GroupBy(i => new { i.LocationId, i.ProductId })
                            .Select(g => new InventoryAvailableQuantity
                            {
                                ProductId = g.Key.ProductId,
                                LocationId = g.Key.LocationId,
                                Product = g.FirstOrDefault().Product.Name,
                                Location = g.FirstOrDefault().Location.Name,
                                PurchasePrice = AppContext.Inventories.Where(i => i.ProductId == g.Key.ProductId).OrderByDescending(i => i.DateAdded).FirstOrDefault().PurchasePrice,
                                ResellerPrice = AppContext.Inventories.Where(i => i.ProductId == g.Key.ProductId).OrderByDescending(i => i.DateAdded).FirstOrDefault().ResellerPrice,
                                RetailPrice = AppContext.Inventories.Where(i => i.ProductId == g.Key.ProductId).OrderByDescending(i => i.DateAdded).FirstOrDefault().RetailPrice
                            }).ToList();

2 个答案:

答案 0 :(得分:2)

您可以使用理解代替方法,并获得使用“ let”的能力:

var inventories = from inv in AppContext.Inventories
                  group inv by new { i.LocationId, i.ProductId } into g
                  let firstInv = g.FirstOrDefault()
                  let firstPur = AppContext.Inventories
                                        .Where(i => i.ProductId == g.Key.ProductId)
                                        .OrderByDescending(i => i.DateAdded)
                                        .FirstOrDefault()
                  select new InventoryAvailableQuantity
                  {
                      ProductId = g.Key.ProductId,
                      LocationId = g.Key.LocationId,
                      Product = firstInv.Product.Name,
                      Location = firstInv.Location.Name,
                      PurchasePrice = firstPur.PurchasePrice,
                      ResellerPrice = firstPur.ResellerPrice,
                      RetailPrice = firstPur.RetailPrice
                  }; // ( select ... { ... }).ToList(); if you will

答案 1 :(得分:0)

快速答案

        var inventories = Inventories
            .GroupBy(i => new {i.LocationId, i.ProductId})
            .Select(g => new
            {
                g.Key.ProductId,
                g.Key.LocationId,
                CurrentInventories = g.FirstOrDefault(),
                LastInventories = Inventories.Where(i => i.ProductId == g.Key.ProductId).OrderByDescending(i => i.DateAdded).FirstOrDefault()
            })
            .Select(g => new InventoryAvailableQuantity
            {
                ProductId = g.ProductId,
                LocationId = g.LocationId,
                Product = g.CurrentInventories.Product.Name,
                Location = g.CurrentInventories.Location.Name,
                PurchasePrice = g.LastInventories.PurchasePrice,
                ResellerPrice = g.LastInventories.ResellerPrice,
                RetailPrice = g.LastInventories.RetailPrice
            })
            .ToList();

您可以在分组后获取最后一个项目,并获取所需的内容。