Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何改进此LINQ查询?_C#_.net_Linq - Fatal编程技术网

C# 如何改进此LINQ查询?

C# 如何改进此LINQ查询?,c#,.net,linq,C#,.net,Linq,我担心我在这里做的是n+1查询,如何改进 var inventories = AppContext.Inventories .GroupBy(i => new { i.LocationId, i.ProductId }) .Select(g => new InventoryAvailableQuantity {

我担心我在这里做的是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();
快速回答

        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();

你可以在分组后取最后一项,然后取你想要的。

你可以使用理解而不是方法,并获得使用“let”的能力:


这有点模糊。您特别寻求哪些改进?您是否希望n+1查询一次性运行(例如使用联接)?因此,当您查看生成的SQL时,它实际上在做什么?它实际上是执行1个查询还是N个查询?
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