C# - 将列表拆分为n个子列表

时间:2014-01-22 22:44:43

标签: c# linq

我想将列表拆分为'n'个子列表。

我有一份表格教师名单和一份学生名单。每个学生都被分配到一个表格教师,每个表格教师可以有一个以上的学生。表单教师列表是动态的 - 它是根据表单上的复选框选择填充的(即:列表中可能有一个,三个,六个等)。

//A method to assign the Selected Form teachers to the Students
private void AssignFormTeachers(List<FormTeacher> formTeacherList, List<Student> studentList)
{
    int numFormTeachers = formTeacherList.Count;

    //Sort the Students by Course - this ensures cohort identity.
    studentList = studentList.OrderBy(Student => Student.CourseID).ToList();

    //Split the list according to the number of Form teachers
    List<List<Student>> splitStudentList = splitList(numFormTeachers , studentList);

splitList()方法是我试图将列表拆分为学生列表列表的地方,但我遇到了问题。假设有3名表格教师 - 我似乎无法将列表分成3个子列表,而是最终列出3名学生名单。

我真的很感激这方面的一些帮助。我已经搜索了一个可能的解决方案,但每次我都会得到大小为'n'的列表,而不是'n'个列表。如果之前已经回答过这个问题,请指出我的答案。

1 个答案:

答案 0 :(得分:15)

您是否尝试将列表划分为具有相同数量元素的n部分?

尝试GroupBy

var splitStudentList = studentList.Select((s, i) => new { s, i })
                                  .GroupBy(x => x.i % numFormTeachers)
                                  .Select(g => g.Select(x => x.s).ToList())
                                  .ToList();

或者您可以创建自己的扩展方法来执行此操作。我已经在我的博客上描述了如何做到这一点:Partitioning the collection using LINQ: different approaches, different performance, the same result

public IEnumerable<IEnumerable<T>> Partition<T>(IEnumerable<T> source, int size)
{
    var partition = new List<T>(size);
    var counter = 0;

    using (var enumerator = source.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            partition.Add(enumerator.Current);
            counter++;
            if (counter % size == 0)
            {
                yield return partition.ToList();
                partition.Clear();
                counter = 0;
            }
        }

        if (counter != 0)
            yield return partition;
    }
}

用法:

var splitStudentList = studentList.Partition(numFormTeachers)
                                  .Select(x => x.ToList())
                                  .ToList();