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

System.Linq和IEnumerable组帮助

  •  0
  • WesleyJohnson  · 技术社区  · 15 年前

    我只是用LINQ和IEnumerable把脚弄湿了,我需要帮助确定我的对象是否包含纸牌游戏的匹配项。我想如果我先弄清楚第一个,我需要做的另一个匹配检查就会到位。

    public class Card
    {
        pubic int Value { get; set; }
        public Card(int value)
        {
            this.Value = value;
        }
    }
    
    public bool IsCompletedSet(List<Card> cards)
    {
        var cardValueGroups = cards.GroupBy(card => card.Value);
        //How do I determine that there are now exactly two groups of cards
        //and that each group contains exactly 3 cards each?
    }
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Derek Slager    15 年前

    要获取组数:

    cardValueGroups.Count()
    

    确保他们都有三张牌:

    cardValueGroups.All(g => g.Count() == 3)