views:

44

answers:

1

I'm just getting my feet wet with Linq and IEnumerable, and I'm needing help in trying to determine if my objects contain matches for a card game. I think if I get the first one figured out, the other match checking I need to do will fall in place.

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?
}
+2  A: 

To get the number of groups:

cardValueGroups.Count()

To make sure they all have exactly three cards:

cardValueGroups.All(g => g.Count() == 3)
nullptr
Excellent, thanks for the quick response. I'll study this to understand what it's doing and then see about applying it to my other match tests. Kudos!
WesleyJohnson
Just to be clear, you should be making these calls on cardValueGroups, since these are meant to be used after the GroupBy has been performed (so you have an enumeration of enumerations).
Chris Patterson
Thanks for the additional info Chris, I figured that out just moments ago when I was trying to mimic this on a single "card" and it wouldn't work. Now I know why! :)
WesleyJohnson