1

我正在尝试进行查询以生成装箱单,并且需要一般订单信息以及订单包含的物品数量。

这是我的基本查询:

var orders = (from cert in _orderRepository.GetAll()
                           where !cert.PrintedPackSlip
                           orderby cert.CardNumber
                           //group cert by cert.CardNumber
                           //into certGrp
                           select cert);

我需要做的是按 CardNumber 分组并计算有多少订单有该卡号。

如果可能的话,我希望它像字典一样出来

4

2 回答 2

1

如果我理解正确,对于每个订单,您要列出该卡号的订单总数,在这种情况下,以下应该有效:

        var orders = (from cert in _orderRepository.GetAll()
                      join cert2 in _orderRepository.GetAll()
                      on cert.CardNumber equals cert2.CardNumber
                      where !cert.PrintedPackSlip 
                      where !cert2.PrintedPackSlip
                      group cert by cert
                      into certGrp
                      select certGrp).ToDictionary(o => o.Key,o => o.Count())
于 2012-07-05T19:39:14.837 回答
1

可能是这样的:

var orders = from cert in _orderRepository.GetAll()
                       where !cert.PrintedPackSlip
                       orderby cert.CardNumber
                       group cert by cert.CardNumber into certGrp
                       select new {CardNumber = certGrp.Key, Count = certGrp.Count());

foreach(var item in orders)
    Console.WriteLine("item.CardNumber: " + item.CardNumber + " item.Count: " + item.Count.ToString());
于 2012-07-05T19:10:52.933 回答