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
答案 0 :(得分:6)
一种方法:
var playerWithHighestGoalRank = data.SpelerCollection
.OrderByDescending(player => player.Goal)
.First();
答案 1 :(得分: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