我如何分组这个查询?

时间:2015-02-03 11:11:45

标签: sql linq linq-to-sql

我想按order.orderID分组。 可能吗? 我尝试但我不能这样做。 有什么想法吗?

   var query = from order in mydb.Orders

                     join customer in mydb.Customers on order.CustomerID equals customer.CustomerID
                     join employee in mydb.Employees on order.EmployeeID equals employee.EmployeeID
                     join shipper in mydb.Shippers on order.ShipVia equals shipper.ShipperID
                     join orderdetail in mydb.Order_Details on order.OrderID equals orderdetail.OrderID
                     join product in mydb.Products on orderdetail.ProductID equals product.ProductID
                     select new
                                 {

                                     OrderID = order.OrderID,
                                     CustomerName = customer.CompanyName,
                                     EmployeeName = employee.FirstName,
                                     ShipperName = shipper.CompanyName,
                                     Products = product.ProductName,
                                     TotalPrice = orderdetail.UnitPrice * orderdetail.Quantity
                                 };

1 个答案:

答案 0 :(得分:0)

只需通过查询延续在表达式的末尾添加group by子句:

var query = from order in mydb.Orders
            join customer in mydb.Customers on order.CustomerID equals customer.CustomerID
            […]
            select new {
              OrderID = order.OrderID,
              CustomerName = customer.CompanyName,
              […]
            }
            into result
            group result by result.OrderId;