【问题标题】:SQL Join table and SUM valuesSQL 连接表和 SUM 值
【发布时间】:2014-09-18 07:34:35
【问题描述】:

我有两张桌子:

产品和库存

我目前选择 Products 的内容并第二次调用 stock 表来总结 productID 的总库存,这非常慢。

我想要创建一个对数据库的单个调用以获取 Product 表的内容,并将 stock 表(链接到 ProductID)中 StockInHand 的总数相加,如下所示:

如果有人能告诉我如何成功地加入表格并在对 ProductID 的同一次调用中对 Stock 表的 QtyInHand 求和,我将非常感激。

我的原始代码:

            var query = from products in data.Products
                        where products.Deleted == false
                        orderby products.FullPath
                        select new
                        {
                            ProductID = products.ProductID,
                            Description = products.Description,
                            Price = products.RetailPrice ?? 0,
>>>> VERY SLOW!             StockLevel = cProducts.GetStockTotalForProduct(products.ProductID), 
                            FullPath = products.FullPath
                        };

            if (query != null)
            {
                dgv.DataSource = query;
            }

我知道我需要加入表,但我不确定使用 LINQ 执行此操作的语法:

    var query = 
                from product in data.Products
                join stock in data.ProductStocks on product.ProductID equals stock.ProductID
 DO SOMETHING CLEVER HERE!!
                select new
                {
                    ProductID = product.ProductID,
                    Description = product.Description,
                    Price = product.RetailPrice ?? 0,
                    StockLevel = >>>>>>>> CALL TO THE OTHER TABLE IS VERY SLOW,
                    FullPath = products.FullPath
                };

            if (query != null)
            {
                dgv.DataSource = query;
            }

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    我想你正在寻找一个group by 来做总和:

    var query = from p in data.Products
                join s in data.ProductStocks on p.ProductID equals s.ProductID
                group s by p into g
                select new {
                    ProductID = g.Key.ProductID,
                    Description = g.Key.Description,
                    Price = g.Key.Price ?? 0,
                    FullPath = g.Key.FullPath,
                    StockLevel = g.Sum(s => s.StockInHand)
                };
    

    【讨论】:

      【解决方案2】:

      我认为这应该可行:

       var query = from product in data.Products
                      join stock in data.ProductStocks on product.ProductID equals stock.ProductID
                      select new
                      {
                          ProductID = product.ProductID,
                          Description = product.Description,
                          Price = product.RetailPrice ?? 0,
                          StockLevel = stock.StockLevel,
                          FullPath = products.FullPath
                      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        • 1970-01-01
        相关资源
        最近更新 更多