【问题标题】:How to GroupBy data in Linq to SQL query如何在 Linq to SQL 查询中对数据进行分组
【发布时间】:2015-01-15 17:20:17
【问题描述】:

在 Linq to SQL 查询中让我的 groupby 正确时遇到了问题。

我有 3 张桌子:CustomersOrdersOrderItemsOrderItems 包含所购买商品的数量和价格。

现在我只想列出所有客户以及所有订单的总价。 我有(在许多其他尝试中)这个,但它没有编译。 Intellisense 中没有 TotalPrice 项目,我在最后一行有 ???

var q = (from c in db.Customers
        join o in db.Orders on c.ID equals o.Customer
        //join oi in db.OrderItems on o.ID equals oi.OrderID
        group c by new {c.CustomerName, TotalPrice = o.OrderItems.Sum(x => x.Quantity * x.Price)} into groupby
        select groupby);

foreach (var cust in q)
    Console.WriteLine("{0}  {1}", cust.Min(x => x.CustomerName), cust.Sum(x => x.???);

(简单、易懂、直接且简单的)SQL 查询将如下所示:

SELECT C.ID, MIN(CustomerName) CustomerName, SUM(Quantity * Price) OrderPrice
FROM Customers C 
        INNER JOIN ORDERS O ON C.ID = O.Customer
        INNER JOIN OrderItems OI ON O.ID = OI.OrderID
GROUP BY C.ID

【问题讨论】:

  • 如果表中有外键,使用 LinQ 可以很简单地完成。你有吗?或者你只知道它是什么领域?
  • 是的,我确实有外键。所以可以使用外键。
  • 实际上你在 SQL 中所做的不是OrderPrice,而是TotalOrderPriceByCustomer,这就是你想要达到的目标吗?我不知道你为什么使用MIN(CustomerName) 而不是将CustomerName 添加到GROUP BY
  • 将 CustomerName 添加到 GroupBy 将获得相同的结果,因为 Name 不是 PK 的一部分。我(可能不正确)的理解是 Min 会执行得更快。是的,我正在寻找 TotalOrderPricePerCustomer。

标签: linq linq-to-sql anonymous-types


【解决方案1】:

这对我有用:

这是客户和订单的总和

var orders = (from c in context.Customers
    join o in context.Orders on c.custid equals o.custid
    join od in context.OrderDetails on o.orderid equals od.orderid
      select new
              {
                  c.custid,
                  o.orderid,
                  od.qty,
                  od.unitprice
              }).GroupBy(c => new
                          {
                              c.custid,
                              c.orderid
                          }).Select(c => new
                          {
                              c.Key.custid,
                              c.Key.orderid,
                              Summ = c.Sum(d => d.qty * d.unitprice)
                          }).ToList();

这只是客户的总和

            var orders = (from c in context.Customers
                          join o in context.Orders on c.custid equals o.custid
                          join od in context.OrderDetails on o.orderid equals od.orderid
                          select new
                          {
                              c.custid,
                              od.qty,
                              od.unitprice
                          }
                          ).GroupBy(c => new
                          {
                              c.custid,
                          }).Select(c => new
                          {
                              c.Key.custid,
                              Summ = c.Sum(d => d.qty * d.unitprice)
                          }).ToList();

【讨论】:

    【解决方案2】:

    我可以乱用名称,但这里是 LinQ 表达式,其 Extension methods 语法与您的 SQL 匹配:

            var q = db.Customers
            .GroupBy(x => new
            {
                x.ID,
                x.CustomerName,
            }, (x, group) => new
            {
                Id = x.ID,
                CustomerName = x.CustomerName,
                TotalPrice = group.Select(y => y.Orders.Sum(z => z.OrderItems.Sum(a => a.Price * a.Quantity)))
            });
    
            foreach (var cust in q)
                Console.WriteLine("{0}  {1}", cust.CustomerName, cust.TotalPrice);
    

    【讨论】:

    • 好吧,我必须说它似乎正在工作,所以我会接受作为答案。解决方案中有几件事我还不了解,但我必须相信那里也必须有一个简单的解决方案。如果我找到一些可以让我绕起来的东西,我会发布:-)。感谢您的回答。
    【解决方案3】:

    这也对我有用:

    var q = (from c in db.Customers
            join o in db.Orders on c.ID equals o.Customer
            join oi in db.OrderItems on o.ID equals oi.OrderID
            group new {c, o.ID, oi.Quantity, oi.Price} by new {c.ID, c.CustomerName} into grp
            let TotalPrice = grp.Sum(x => x.Quantity * x.Price)
            select new 
            {
                CustomerID = grp.Key.ID,
                CustomerName = grp.Key.CustomerName,
                TotalPrice
            });
    

    var q = (from c in db.Customers
                join o in db.Orders on c.ID equals o.Customer
                join oi in db.OrderItems on o.ID equals oi.OrderID
                group new { CustomerID = c.ID, CustomerName = c.CustomerName, Price = oi.Price, Quantity = oi.Quantity} by c.ID into grp
                select new 
                { 
                    ID = grp.Key, 
                    CustomerName = grp.Min(x => x.CustomerName), 
                    TotalPrice = grp.Sum(x => x.Price * x.Quantity)
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多