开发者

How can I get the highest number in this Linq result?

private static void VerifyWinner(int[] PlayerRolls, int[] ComputerRolls)
{
    var playerCombos = from number in PlayerRolls
                       group number by number into n
                       where n.Count() > 1
                       select new { n.Key, Count = n.Count() };

    var computerCombos = from number in ComputerRolls
                         group number by number into n
                         where n.Count() > 1
                         select new { n.Key, Count = n.Count() };
}

Basically, player and computer combos have a keypair values of each number that appears and how many times it appears.

Here's the pickle. I need to grab the number that appears the most from both collections. If there are for example, two pairs (1,1 and 2,2) appearing in the computerCombos collection, I need to grab the combo with the highest number.

Example:

[1,1,2,4,5,6,7,7]

I need to get:

7 appeared twice. 

Notice how I didn't display 1, because 7 is of higher value.

Here my attempt, but it's not working as expected. It fetches whatever the .First method gives.

p

rivate static void VerifyWinner(int[] PlayerRolls, int[] ComputerRolls)
{
    var playerCombos = from number in PlayerRolls
                        group number by number into n
                        where n.Count() > 1
                        select new { n.Key, Count = n.Count() };

    var computerCombos = from number in ComputerRolls
                            group number by number into n
                            where n.Count() > 1
                            select new { n.Key, Count = n.Count() };

    var p = playerCombos.First();
    var c = computerCombos.First();

    if (p.Count > c.Count)
    {
        Console.WriteLine("Player wins with a {0} card combo!", p.Count);
    }
    else if (p.Count < c.Count)
    {
        Console.WriteLine("Computer wins with a {0} card combo!", c.Count);
    }
    else
    {
        Console.WriteLine("The match was a draw!");
   开发者_Go百科 }
}

Thanks!


Try this:

 var p = playerCombos.OrderByDescending(x => x.Count)
                     .ThenByDescending(x=>x.Key)
                     .First();
 var c = computerCombos.OrderByDescending(x => x.Count)
                       .ThenByDescending(x=>x.Key)
                       .First();

This is more readable in query format actually:

var p = (from combo in playerCombos
         orderby combo.Count descending, combo.Key descending
         select combo).First();


Try:

    var p = playerCombos.Where(
                    x => x.Count == 
                        playerCombos.Max(y => y.Count)
                    ).Max(x => x.Key)


Similar to Jon's answer, but to remove the nested operator I'd shift the calculation of the Max outside the where.

var comboMax = (from combo in playerCombos
                let max = playerCombos.Max(c => c.Count)
                where combo.Count == max
                select combo)
                   .Max(combo => combo.Key);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜