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

我正在为一些编程实践做一个扑克手评估器,我已经提交给codereview堆栈交换.我需要能够正确地比较双手,为此我需要看到对的价值.我目前检查双手班

 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;

比较完美的比较高卡问题,但我需要添加一个检查,如果两个手牌等级,然后检查他们的价值是一对,两对,满屋或三种(然后找到最高的面值的一对)

如果您需要有关我的程序的更多信息,请随时查看https://codereview.stackexchange.com/questions/152857/beginnings-of-a-poker-hand-classifier-part-2?noredirect=1&lq=1后的codereview

最佳答案 这可能不是您正在寻找的,因为您正在比较对象而不仅仅是整数,但这可以帮助您开始.基于这个问题:
How to get frequency of elements stored in a list in C#.

using System.Linq;

List<int> ids = //
int maxFrequency = 0;
int IDOfMax = 0;

foreach(var grp in ids.GroupBy(i => i))
{
    if (grp.Count() > maxFrequency) 
    {
        maxFrequency = grp.Count();
        IDOfMax = grp.Key;
    }
}

// The object (int in this case) that appears most frequently 
// can be identified with grp.key

更新:在重新阅读问题之后,听起来您需要尝试使用查询中的计数和面值返回一个新对象.

你可以这样做:

public class FaceCountResult
{
    public int Count { get; set; }
    public Face FaceValue { get; set; }

    public FaceCountResult(int count, Face faceValue)
    {
        Count = count;
        FaceValue = faceValue;
    }
}

然后,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] .Count上切换并使用faceCount [0] .FaceValue来获取面值.

点赞