【问题标题】:Linq - Getting object with enum where clauseLinq - 使用枚举 where 子句获取对象
【发布时间】:2018-03-12 20:53:25
【问题描述】:

我正在制作一个团队可以“选拔”球员的程序。与其让球队选择最好的球员,我希望球队选择最好的球员(总体而言),在球队拥有最少球员的位置上踢球。

播放器类:

public enum Position { PG,SG,SF,PF,C};

    public string Name { get; private set; }
    public Position Position { get; private set; }
    public int Id { get;}
    public int TeamId { get;}
    public int Overall { get; private set; }
    public int InsideScoring { get; private set; }
    public int MidScoring { get; private set; }
    public int ThreeScoring { get; private set; }
    public int Passing { get; private set; }
    public int BallHandling { get; private set; }
    public int PerimeterD { get; private set; }
    public int InsideD { get; private set; }
    public int Rebounding { get; private set; }
    public int Steals { get; private set; }
    public int Blocks { get; private set; }
    public int Strength { get; private set; }

团队类

    public int Id { get; set; }
    public string Hometown { get; set; }
    public string Teamname { get; set; }
    public Color Teamcolor { get; set; }
    public List<Player> teamPlayers { get; set; } = new List<Player>();

在我的 Draft 课程中,我有以下“相关”代码。

void getPlayers()
    {
        allPlayers = new List<Player>();
        allPlayers = sql.Select("Select * from player");
        var source = new BindingSource();
        source.DataSource = allPlayers;
        dgAllPlayers.DataSource = source;
        dgAllPlayers.AutoGenerateColumns = true;
    }
 Team nextTeam;
 List<Player> allPlayers;
 void nextUP()
        {
            if (nextTeam.UserControlled == 0)
            {
                Player ChosenPlayer = aiChoose(nextTeam);
                nextTeam.teamPlayers.Add(ChosenPlayer);
                allPlayers.Remove(ChosenPlayer);
                dgAllPlayers.DataSource = null;
                dgAllPlayers.DataSource = allPlayers;
            }
             nextUP();
        }

    private Player aiChoose(Team team)
    {
        //get best player available
        Player ChosenPlayer = allPlayers.MaxBy(x => x.Overall);
        return ChosenPlayer;
    }

所以在 aiChoose 方法中我的 Linq Query 应该被替换。我很清楚这也可以通过使用 forloops 来实现,但我认为使用 Linq 会更好,对吧?

【问题讨论】:

  • allPlayers.OrderByDescending(p =&gt; p.Overall).FirstOrDefault() MaxBy 的意思吗?
  • 你试过allPlayers.OrderByDescending(x =&gt; x.Overall).FirstOrDefault();吗?
  • @vc74 是的,我在我的项目中使用 moreLinq
  • 我不清楚问题是什么。

标签: c# linq


【解决方案1】:

让最好的球员作为随机剩余位置上场:

Random random = new Random();
Position[] allPositions = Enum.GetValues(typeof(Position)) as Position[];

private Player ChooseNextPlayer(Team team)
{
    var positionsToAllocate = allPositions.Except(team.TeamPlayers.Select(p => p.Position));
    var randomNotAllocatedPosition = positionsToAllocate.ElementAt(random.Next(positionsToAllocate.Count()));

    return allPlayers.Where(p => p.Position == randomNotAllocatedPosition).MaxBy(p => p.Overall);
}

【讨论】:

  • 抱歉完全忘记在代码中添加我的位置枚举,请查看编辑
  • 字段“p”是什么意思?
  • 我用 Position 和 Where part fixed 更新了我的答案,这对你有意义吗?
  • 我不认为我的问题很清楚。有5个可能的位置:PG,SG,SF,PF,C。想象一个球队已经有以下位置的球员:1个PG,1个PF和1个C。所以下一个球员的位置应该是SG或SF,随机的。然后它应该从 allPlayers 中选择那个位置并且总分最高的玩家
猜你喜欢
  • 1970-01-01
  • 2018-03-10
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
相关资源
最近更新 更多