如何在Linq中将GroupBy数据转换为SQL查询

时间:2015-01-14 12:25:24

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

我遇到了在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

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

我可以搞乱名字,但这里的 LinQ 表达式与扩展方法语法匹配 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);

答案 2 :(得分:0)

这对我也有用:

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