代码之家  ›  专栏  ›  技术社区  ›  Michel

林肯(groupBy?)问题

  •  1
  • Michel  · 技术社区  · 16 年前

    我收集了 Order 对象,以及每个 命令 对象有一个或多个 Card 其中的对象(在 generic List object called Cards .

    现在我想知道一张牌的总数。

    我没有把它组合起来,而Linq智能感知对我来说毫无意义。。。。

    4 回复  |  直到 16 年前
        1
  •  4
  •   Jens    16 年前

    你可以使用

    yourCollection.Sum(o => o.Cards.Count);
    
        2
  •  2
  •   Marc Gravell    16 年前
    int totalCount = orders.Sum(order => order.Cards.Count);
    
        3
  •  2
  •   Saeed Amiri    16 年前

    可能是卡为空:

    orders.Sum(p => p.Cards == null?0:p.Cards.Count);
    
        4
  •  1
  •   abatishchev Karl Johan    16 年前

    让你拥有:

    class Order
    {
         public List<Card> Cards { get; set; }
    }
    

    然后:

    IEnumerable<Order> orders = ..
    int cards = orders.Sum(o => o.Cards.Count);