在列表中移动对象位置

时间:2018-01-24 15:58:47

标签: c#

我有一大堆有poolNum和poolStanding的玩家。 选择poolSize后(从下拉菜单中),它必须相应地移动玩家。 如果第一个游泳池有2个玩家并且我们希望它有6个玩家,它需要后续游泳池的玩家并相应地改变他们的poolNum和poolStanding。 我没有粘贴我的代码,因为它与问题有无关的信息,但我在下面总结了它。请帮忙。 问候 编辑:下拉菜单仅包含选项2-6。从较低的游泳池移动到较高的游泳池的玩家总是占据上游游泳池的最后一个排名。

Rearrange(int poolNum, int poolSize, list<Participants> participantsList)
participantsList
{
player1 | pool 1| standing 1
player2 | pool 1| standing 2

player3 | pool 2| standing 1
player4 | pool 2| standing 2

player5 | pool 3| standing 1
player6 | pool 3| standing 2
player7 | pool 3| standing 3

player8 | pool 4| standing 1
player9 | pool 4| standing 2
player10 | pool 4| standing 3

player11 | pool 5| standing 1
player12 | pool 5| standing 2
player13 | pool 5| standing 3

}

我们选择在第一个泳池中有6名球员,所以我们必须从下一个泳池中选出4名球员。 2号泳池只有2名玩家,所以我们从第3泳池中取出2名玩家 - &gt; .OrderBy(x =&gt; x.poolNum).ThenBy(x =&gt; x.standing).Take(poolSize - poolNum.Players.Count(//这意味着我们需要4个玩家,因为poolSize(6) - poolNum。 Players.Count(2) - &gt;更新后的列表现在看起来像这样 - &gt;

participantsList
{
player1 | pool 1| standing 1
player2 | pool 1| standing 2
player3 | pool 1| standing 3
player4 | pool 1| standing 4
player5 | pool 1| standing 5
player6 | pool 1| standing 6

player7 | pool 3| standing 3

player8 | pool 4| standing 1
player9 | pool 4| standing 2
player10 | pool 4| standing 3

player11 | pool 5| standing 1
player12 | pool 5| standing 2
player13 | pool 5| standing 3
}

如何将所有后续池1向下移动,将第3池的排名向下移动2?

它看起来像这样 - &gt;

participantsList
{
player1 | pool 1| standing 1
player2 | pool 1| standing 2
player3 | pool 1| standing 3
player4 | pool 1| standing 4
player5 | pool 1| standing 5
player6 | pool 1| standing 6

player7 | pool 2| standing 1

player8 | pool 3| standing 1
player9 | pool 3| standing 2
player10 | pool 3| standing 3

player11 | pool 4| standing 1
player12 | pool 4| standing 2
player13 | pool 4| standing 3
}

函数Rearrange接收我想要修改的池的ID,我们需要它的SIZE以及存储来自所有池的所有玩家的List。 每个对象都有一个id(playerN),它所属的池(poolN)和一个站在该池(standingN)。 通过向上/向下移动玩家,我必须修改其他玩家的游泳池N和站立N.

以下是我的代码:

public void ReorderParticipants(int poolOrder, int poolSize, List<ClubLeagueParticipant> allPlayersFromClubLeague)
{
    int playersInPool = allPlayersFromClubLeague
        .Where(x => x.lp_pool_order == poolOrder)
        .Count();     

    int movingPlayers = poolSize - playersInPool;

    var playersToMoveDown = allPlayersFromClubLeague
        .Where(x => x.lp_pool_order == poolOrder && x.lp_standing > poolSize)
        .ToList();

    var playersToMoveUp = allPlayersFromClubLeague
        .Where(x => x.lp_pool_order > poolOrder)
        .OrderBy(x => x.lp_pool_order)
        .ThenBy(x => x.lp_standing)
        .Take(movingPlayers)
        .ToList();

    var standingsToPush = allPlayersFromClubLeague
        .Where(x => x.lp_pool_order == poolOrder + 1)
        .ToList();

    var standingsToPull = allPlayersFromClubLeague
        .Where(x => x.lp_pool_order > poolOrder)
        .OrderBy(x => x.lp_pool_order)
        .ThenBy(x => x.lp_standing)
        .Take(movingPlayers)
        .ToList();

    if (playersToMoveDown.Count != 0)
    { 
        foreach (var standing in standingsToPush)
        {
            standing.lp_standing += playersToMoveDown.Count;
        }

        for (int i = 0; i <= playersToMoveDown.Count; i++)
        {
            playersToMoveDown[i].lp_pool_order += 1;
        }
    }

    if (playersToMoveUp.Count != 0)
    { 
        foreach (var standing in standingsToPull)
        {
            standing.lp_standing += playersInPool;
        }

        for (int i = 0; i <= playersToMoveUp.Count; i++)
        {
            playersToMoveUp[i].lp_pool_order -= 1;
        }               
    }       
}

当推动球员向下时 - 一切都很好,但是当他们将球队拉到上游球场时,留在球场中的球员已经从中被提取出来,在获得球员之前仍然保持着原有的排名。

1 个答案:

答案 0 :(得分:0)

我无法解决您的问题,因为我对要求知之甚少(例如,如果您使用以下方法调用该函数:

ReorderParticipants(1, 6, test);

您似乎暗示将Player 7移动到池2,但这意味着通过更改池1的大小,yoy也会更改池2的大小。

但是,您的代码中存在一些逻辑错误,可能会让您陷入困境:

if (playersToMoveUp.Count != 0)
{
    foreach (var p in playersToMoveUp)
    {
        p.lp_pool_order = poolOrder;
    }
}

int tmpPoolSize = playersInPool + 1;
foreach (var standing in standingsToPull)
{
    standing.lp_standing = tmpPoolSize++;
}