C# 试图在球员名单中获得最高的进球率,进球数

C# 试图在球员名单中获得最高的进球率,进球数,c#,C#,现在我想用一个按钮显示最高的进球得分者。我试着使用maxvalue等等 private void goalButton_Click(object sender, EventArgs e) { if (ploeg1ListBox.SelectedIndex >= 1) { Player pl = (Player)team1ListBox.SelectedItem; pl.player++;

现在我想用一个按钮显示最高的进球得分者。我试着使用maxvalue等等

    private void goalButton_Click(object sender, EventArgs e)
    {
        if (ploeg1ListBox.SelectedIndex >= 1)
        {
            Player pl = (Player)team1ListBox.SelectedItem;
            pl.player++;
            GoalForm goal = new GoalForm(); //winform with picture
            goal.ShowDialog();
        }
我似乎找不到适合我的小程序的代码,你们能帮我吗

Grts

为什么不:

  foreach(Speler sp in data.SpelerCollection)
  {
      for (int counter = 0; counter > data.SpelerCollection.Count;counter++)
      {
一种方法是:

var maxGoals = SpelerCollection.Max( s => s.Goal);
Speler maxScorer = SpelerCollection.Where( s => s.Goal == maxGoals).First();
// rest of your logic ...
// you should handle the case, when more than one
// player have scored the same amount of goals.
// It would be better to get a collection back and then
// display the result depending on the number of players returned

如果你有林克,你可以用马克斯来获得最高得分的球员。我正要评论说单曲可能不是最好的。我只想为最高得分者或联合最高得分者返回一个IEnumerable。@Chris你是对的,但他只要求一个对象。但是op可以很容易地将答案更改为一个集合。我更喜欢你的第一个单曲,不管怎样,我在单曲中遇到的问题是可能出现例外@克里斯,是的,这也改变了-如果列表为空,First可以引发异常。FirstOrDefault将返回第一项,如果结果为空,则返回null defaultT。
var maxGoals = SpelerCollection.Max( s => s.Goal);
Speler maxScorer = SpelerCollection.Where( s => s.Goal == maxGoals).First();
// rest of your logic ...
// you should handle the case, when more than one
// player have scored the same amount of goals.
// It would be better to get a collection back and then
// display the result depending on the number of players returned
var playerWithHighestGoalRank = data.SpelerCollection
                                        .OrderByDescending(player => player.Goal)
                                        .First();