2016-12-22 7 views
0
public class Player 
{ 
    public string Name { get; private set; } 
    public string Firstname { get; private set; } 
    public Ploeg Team { get; private set; } 
    public int Goal { get; set; } 
} 
public class GlobalCollection 
{ 
    public List<Ploeg> PloegCollection { get; private set; } 
    public List<Speler> SpelerCollection { get; private set; } 
} 
public GlobalCollection() 
{ 
     SpelerCollection.Add(new Speler("Stanciu", "Nicolae", p1 , 0)); 
     SpelerCollection.Add(new Speler("Massimo", "Bruno", p1 , 0)); 
     SpelerCollection.Add(new Speler("Hanni", "Sofiane", p1 , 0)); 
     SpelerCollection.Add(new Speler("Teodorczyk", "Lukasz", p1, 0)); 
} 

私はゴール+1を設定するボタンを持っています。選手のリストで最高の得点を得ようとしています

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

ここでは、最高のゴールのスコアラーをクリックすると表示されます。私はMAXVALUEを使用してみましたので、..に

foreach(Speler sp in data.SpelerCollection) 
    { 
     for (int counter = 0; counter > data.SpelerCollection.Count;counter++) 
     { 

を私は私の小さなプログラムのための右のコードを見つけることができないよう、あなたは私の男を助けることができますか?

Grtsないのはなぜ

+1

あなたは最高得点のプレイヤーを取得するには、 'Max'を使用することができます。 – nbokmans

答えて

4

:それを行うの

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 
+1

シングルは恐らく最高のものではないとコメントしようとしていた。私は、最高または最高のスコアラーのために 'IEnumerable 'を返すようにしておきます。 – Chris

+0

@Chrisあなたは正しいですが、彼は単一のオブジェクトを求めました。しかし、オペレーションは、コレクションに対する答えをかなり簡単に変更することができます。 – Mithrandir

+1

私はあなたの '第一'を 'シングル'にするのが好きです(私は 'シングル 'が例外の可能性を秘めているという問題でした)。 :) – Chris

6

一つの方法:あなたは、LINQを使用している場合

var playerWithHighestGoalRank = data.SpelerCollection 
             .OrderByDescending(player => player.Goal) 
             .First(); 
関連する問題