score:1

Accepted answer

This probably isn't exactly what you're looking for, since you're comparing objects and not just ints, but this could help you get started. Based on this question here: 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

Update: after rereading the question, it sounds like you need to try to return a new object with the count and the face value from your query.

You can do this like so:

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

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

Then, faceCount should look something like this:

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);

I'm not sure how the Take(2) part would factor into this, as I don't quite understand that part of the code.

You can then do a switch on faceCount[0].Count and use faceCount[0].FaceValue to get the face value.


Related Articles