public bool IsValid() { FirstPairOfCards = new ICard[0]; SecondPairOfCards = new ICard[0]; IEnumerable <CardRank> values = Cards.Select(x => x.Rank); IEnumerable <CardRank> cardRanks = values as CardRank[] ?? values.ToArray(); IEnumerable <IGrouping <CardRank, CardRank> > grouped = cardRanks.GroupBy(x => x).ToArray(); if (grouped.Count() != 3) { return(false); } // ReSharper disable PossibleMultipleEnumeration // todo try to remove ReSharper comment foreach (IGrouping <CardRank, CardRank> grouping in grouped) { int count = grouping.Count(); if (count == 1) { HighestCard = Cards.First(x => x.Rank == grouping.Key); } if (count != 2) { continue; } if (!FirstPairOfCards.Any()) { FirstPairOfCards = Cards.Where(x => x.Rank == grouping.Key); } else { SecondPairOfCards = Cards.Where(x => x.Rank == grouping.Key); } } return(FirstPairOfCards.Any() && SecondPairOfCards.Any()); // ReSharper restore PossibleMultipleEnumeration }