System.Linq and IEnumerable Group Help
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 开发者_StackOverflow社区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?
}
To get the number of groups:
cardValueGroups.Count()
To make sure they all have exactly three cards:
cardValueGroups.All(g => g.Count() == 3)
精彩评论