0

我想在网格中的订单信息附近显示 OrderDetails Count,但在选择单元中我只能选择键和计数。如何选择订单信息?

var Q = from Data in Context.Orders
        join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
        group Data by Data.OrderID into grouped
                       select new
                              {
                                  grouped=g.Key,
                                  Count = grouped.Count() 
                              };
4

3 回答 3

2

您可以按整个订单实体将其分组,例如

var Q = from Data in Context.Orders
        join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
        group Data by Data into grouped
                       select new
                              {
                                  OrderId = grouped.Key.OrderId,
                                  OrderDate = grouped.Key.OrderDate
                                  Shipping = grouped.Key.Shipping
                                  .
                                  .
                                  .
                                  Count = grouped.Count() 
                              };

编辑Linqpad 程序,用于在对象的内存集合中进行类似查询

void Main()
{
    var orders = new List<Order>{
        new Order{OrderId = 1, DeliverIn = 5},
        new Order{OrderId = 2, DeliverIn = 6},
        new Order{OrderId = 3, DeliverIn = 5},
    };

    var lines = new List<OrderLine>{
      new OrderLine{LineId = 1, OrderId = 1, ProductId = 1},
      new OrderLine{LineId = 2, OrderId = 1, ProductId = 2},
      new OrderLine{LineId = 3, OrderId = 1, ProductId = 3},

      new OrderLine{LineId = 4, OrderId = 2, ProductId = 1},
      new OrderLine{LineId = 5, OrderId = 2, ProductId = 3},
      new OrderLine{LineId = 6, OrderId = 2, ProductId = 4},
    };

    var query = from o in orders join l in lines on
            o.OrderId equals l.OrderId
            group o by o into grouped
            select new 
            {
                Count = grouped.Count(),
                grouped.Key.OrderId,
                grouped.Key.DeliverIn
            };
            Console.WriteLine(query);
}

// Define other methods and classes here
public class Order
{
    public int OrderId{get;set;}
    public int DeliverIn{get;set;}

}

public class OrderLine
{
    public int LineId{get;set;}
    public int OrderId{get;set;}
    public int ProductId{get;set;}
}

如果您没有 linq pad,只需从他们的网站上获取它。这简直太棒了。

于 2012-05-24T05:29:58.550 回答
1

查看MSDN 上的IGrouping文档。

public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, 
IEnumerable

注意IEnumerable。Count 只是 IEnumerable 的扩展方法。您可以轻松地从分组中选择或循环遍历它。

例如:

var Q = from Data in Context.Orders
    join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
    group Data by Data.OrderID into grouped
                   select new
                          {
                              grouped=g.Key,
                              Count = grouped.Count(),
                              Orders = grouped.ToArray() 
//you can also just return grouped itself to support lazy queries
                          };
于 2012-05-24T05:24:00.710 回答
0

只需将它们展平为数组或列表,然后获取其计数。

select new
{
    Key = g.Key,
    Orders = grouped.ToArray()
};

然后得到计数:

int count = result.Orders.Count; // Property of an array.
于 2012-05-24T05:39:16.203 回答