使用 Linq 计数

本文关键字:计数 Linq 使用 | 更新日期: 2023-09-27 18:35:21

我想在网格中的订单信息附近显示订单详细信息计数,但在选择单元中,我只能选择键和计数。选择订单信息的方法是什么?

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

使用 Linq 计数

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

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,只需从他们的网站上获取它。这简直太棒了。

查看 MSDN 上的 IGrouping 文档。

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

注意IEnumerable。计数只是 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
                          };

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

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

然后获取计数:

int count = result.Orders.Count; // Property of an array.