【问题标题】:How i can group this query?我如何对这个查询进行分组?
【发布时间】:2015-02-03 11:11:44
【问题描述】:

我想按 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
                                 };

【问题讨论】:

  • 分组what by orderId?
  • @Richard 组重复数据
  • 什么数据? customeremployee,……还是别的什么?您的示例代码中有许多数据项。就该代码而言要明确,否则我们必须猜测。
  • 您的代码中没有任何名为 all 的内容。让人们轻松回答您的问题,您可以免费获得帮助,所以请付出一些努力。
  • @Richard 我想要团体客户、员工、托运人、订单详情和产品

标签: sql linq linq-to-sql


【解决方案1】:

只需通过查询延续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;

【讨论】:

  • 很好。但我在显示数据时出错stackoverflow.com/questions/28299708/…
  • @user4523894 查看我对这个问题的回答。 但是请在那个问题中清楚地定义你的要求是用一个例子,直到那时你的意图还不清楚。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多