【问题标题】:This is my SQL query that is used to retrieve the sum of quantity where product has same id, I want to use it in LINQ这是我的 SQL 查询,用于检索产品具有相同 ID 的数量总和,我想在 LINQ 中使用它
【发布时间】:2016-01-04 19:06:45
【问题描述】:

这是我的 SQL 查询,用于检索数量总和,其中 产品具有相同的 id,我想在 LINQ 中使用它。

select p.ProductId,s.TotalQuantity,p.Title from Products p
    join (SELECT ProductId, SUM(Quantity) AS TotalQuantity
            FROM SalePerProduct s join Invoices i on s.InvoiceId = i.InvoiceId
            where i.IssueDate like '%2016%'
            GROUP BY s.ProductId) s
    on s.ProductId = p.ProductId;

我已经尝试过这个 LINQ 查询,但它不能正常工作,它总结了具有相同产品 ID 的数量,但我无法使用它显示产品标题,需要一些帮助。 提前谢谢你。

 var sales = from sale in db.SalePerProducts
                        join product in db.Products
                        on sale.ProductId equals product.ProductId
                        where sale.ProductId == product.ProductId
                        group sale by sale.ProductId into g
                        select new
                        {
                            ProductId = g.Key,
                            Sum = g.Sum(sale => sale.Quantity),
                        };

【问题讨论】:

  • 所以你想看看有多少相同的 id 产品?只是为了确保
  • 我想总结所有具有相同ID的产品,如果它们的ID相同,那么它们的标题也应该相同。例如如果我有两条记录: 1- 15 巧克力 200 5 2- 15 巧克力 200 10 它应该变成 15 巧克力 15

标签: c# linq linq-to-sql sql-to-linq-conversion


【解决方案1】:

这是答案,我终于可以检索多个具有相同名称和 PRODUCT_ID 的产品作为一条记录并总结了数量,谢谢大家。 如果你觉得这有用,请投票

var sales = from sale in db.SalePerProducts
                        join product in db.Products
                        on sale.ProductId equals product.ProductId
                        join invoice in db.Invoices
                        on sale.InvoiceId equals invoice.InvoiceId
                        where sale.ProductId == product.ProductId &&
                                invoice.IssueDate.Date == date
                        group sale by new { sale.ProductId,product.Title } into g
                        select new
                        {
                            ProductId = g.Key.ProductId,
                            Product_Title = g.Key.Title,
                            Quantity_Sold = g.Sum(sale => sale.Quantity)
                        };

【讨论】:

    【解决方案2】:
    var sales = from sale in db.SalePerProducts
                        join product in db.Products
                        on sale.ProductId equals product.ProductId                       
                        group new {sale, product} by sale.ProductId into g
                        select new
                        {
                            ProductId = g.Key,
                            Title = g.FirstOrDefault().product.Title,
                            Sum = g.Sum(s => s.sale.Quantity),
                        };
    

    我删除了不必要的 where 子句,你的版本中有。

    【讨论】:

    • 在我的情况下这不是必需的,因为在 where 子句中我将其与发布日期相匹配,非常感谢您的反馈 :)
    • 对不起,我的版本与您原始问题中的 linq 查询有关。我没有注意到 sql 查询的区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多