简体   繁体   English

从C#中的对象列表创建组

[英]Create Groups from a list of Objects in C#

I Have a list of objects called Team and I want to create groups of them but not in order, randomized. 我有一个称为“团队”的对象列表,我想创建它们的组,但不按顺序随机分配。 The groups number is given to the function as well as the list 组号既提供给功能又提供给列表

public List<List<Team>> GenerateGroups(List<Team> teams, int amount)
{
    List<List<Team>> result = new List<List<Team>>();
    for (int i = 0; i < amount; ++i)
        result.Add(new List<Team>());
    foreach(Team team in teams)
    {
        //Add something           
    }
    return result;
}

i got stuck here. 我被困在这里。 I am not sure how to add the teams. 我不确定如何添加团队。 Also if someone can condense my code, it will be pretty helpful. 另外,如果有人可以压缩我的代码,那将非常有帮助。 I don't need it to return List> if someone has a better idea. 如果有人有更好的主意,我不需要它返回List>。

This will order your teams randomly and then put them in groups. 这将随机命令您的teams ,然后将它们分组。 The number of output groups in the list is according to the amount parameter: 列表中输出组的amount取决于amount参数:

public static List<List<Team>> GenerateGroups(List<Team> teams, int amount)
{
    return teams.OrderBy(item => Guid.NewGuid())
            .Select((item, index) => new { Item = item, GroupIndex = index % amount })
            .GroupBy(item => item.GroupIndex, 
                     (key, group) => group.Select(groupItem => groupItem.Item).ToList())
            .ToList();
}

One can also random using the Random class but I would use it to create the order and not group by it. 也可以使用Random类进行Random但我将使用它来创建订单而不是按其group by Like this: 像这样:

public static List<List<Team>> GenerateGroups(List<Team> teams, int amount)
{
    Random random = new Random();
    return teams.OrderBy(item => random.NextDouble())
            .Select((item, index) => new { Item = item, GroupIndex = index % amount })
            .GroupBy(item => item.GroupIndex,
                     (key, group) => group.Select(groupItem => groupItem.Item).ToList())
            .ToList();
}

I think it's better for you to define Group class like: 我认为最好像下面这样定义Group类:

class Group {
   List<Team> teams;
}

And about adding teams: 关于添加团队:

Random rnd = new Random();
List<Group> groups = new List<Group>();
        for (int i = 0; i < amount; ++i)
            groups.Add(new Group());

        foreach(Team team in teams)
        {
            int index = rnd.Next(0, amount);
            groups[index].Add(team);
        }
        return groups;

This one gives random groups without duplicates: 这给出了随机组,没有重复项:

private static Random _rnd = new Random();

private static Team GetAndRemoveRandomTeam(List<Team> allTeams)
{
    int randomIndex = _rnd.Next(allTeams.Count);
    Team randomTeam = allTeams[randomIndex];
    allTeams.RemoveAt(randomIndex);
    return randomTeam;
}

public static List<List<Team>> GenerateGroups(List<Team> teams, int amount)
{
    int teamCount = (int) teams.Count/amount;
    List<Team> allteams = teams.ToList(); // copy to be able to remove items

    if (teamCount == 0)
        return new List<List<Team>> {allteams};

    List<List<Team>> allTeamGroups = new List<List<Team>>();
    List<Team> thisTeam = new List<Team>();
    while (allteams.Count > 0)
    {
        if (thisTeam.Count == amount)
        {
            allTeamGroups.Add(thisTeam);
            thisTeam = new List<Team>();
        }
        thisTeam.Add(GetAndRemoveRandomTeam(allteams));
    }
    allTeamGroups.Add(thisTeam);

    return allTeamGroups;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM