C# 如何查找同一对象在列表中出现的次数,然后查找出现次数最多的对象的属性

C# 如何查找同一对象在列表中出现的次数,然后查找出现次数最多的对象的属性,c#,linq,poker,C#,Linq,Poker,我正在为我提交给codereview stack exchange的一些编程实践制作一个扑克手评估器。我需要能够正确地比较手,为此我需要看到成对的值。我目前的结对班支票 private static PokerHandsRank CheckHandForPairs(Hand hand) { var faceCount = (from card in hand.Cards group card by card.Face

我正在为我提交给codereview stack exchange的一些编程实践制作一个扑克手评估器。我需要能够正确地比较手,为此我需要看到成对的值。我目前的结对班支票

 private static PokerHandsRank CheckHandForPairs(Hand hand)

    {
        var faceCount = (from card in hand.Cards
                         group card by card.Face
                         into g
                         let count = g.Count()
                         orderby count descending
                         select count).Take(2).ToList(); // take two to check if multiple pairs of pairs, if second in list is 1 there will be two pairs

        switch (faceCount[0])
        {
            case 1: return PokerHandsRank.HighCard;
            case 2: return faceCount[1] == 1 ? PokerHandsRank.Pair : PokerHandsRank.TwoPair;
            case 3: return faceCount[1] == 1 ? PokerHandsRank.ThreeOfKind : PokerHandsRank.FullHouse;
            case 4: return PokerHandsRank.FourOfKind;
            default: throw new Exception("something went wrong here");
        }
    }
正如你所看到的,我已经使用linq获得了出现次数最多的配对的列表,但是我不知道如何在分离它们之后完成它以获得卡片的面值

这是我目前的比较方法

 public int CompareTo(Hand other)
    {
        if (HandRank == other.HandRank) //if the hand rank is equal, sort the cards by face value and compare the two biggest
        {
            sortHandbyFace(this); // sorts cards into order by face
            sortHandbyFace(other);
            for (int i = 4; 0 <= i; i--)
            {
                if (Cards[i].Face == other.Cards[i].Face)
                {
                    if (i == 0) return 0;
                    continue;
                }
                return Cards[i].Face > other.Cards[i].Face ? 1 : -1;
            }              
        }
        return HandRank > other.HandRank ? 1 : -1;
public int CompareTo(另一只手)
{
if(handlank==other.handlank)//如果手级相等,则按面值对卡片进行排序,并比较两张最大的卡片
{
sortHandbyFace(此);//按面将卡片排序
sortHandbyFace(其他);
对于(int i=4;0其他.Cards[i]。面?1:-1;
}              
}
返回HandRank>other.HandRank?1:-1;
compare to非常适合比较高级卡发行,但是我需要添加一个检查,检查两个手牌级别是否相等,然后检查它们的值是一对、两对、fullhouse还是三种(然后找到这对手牌的最高面值)


如果您需要有关我的程序的更多信息,请随时查看我的codereview帖子,这可能不是您想要的,因为您正在比较
对象
s,而不仅仅是
int
s,但这可以帮助您开始。基于此问题:

然后,
faceCount
应该如下所示:

var faceCount = (from card in hand.Cards
                 group card by card.Face
                 into g
                 let count = g.Count()
                 orderby count descending
                 select new FaceCountResult(count, card.Face);
我不确定
Take(2)
部分将如何考虑到这一点,因为我不太理解这部分代码


然后,您可以在
faceCount[0]上执行
切换
。计数
并使用
faceCount[0]。FaceValue
获取面值。

以下内容应该可以帮助您,基本上您只需递增计数不同的卡欢迎来到短码功能强大的linq;)
public class FaceCountResult
{
    public int Count { get; set; }
    public Face FaceValue { get; set; }

    public FaceCountResult(int count, Face faceValue)
    {
        Count = count;
        FaceValue = faceValue;
    }
}
var faceCount = (from card in hand.Cards
                 group card by card.Face
                 into g
                 let count = g.Count()
                 orderby count descending
                 select new FaceCountResult(count, card.Face);