C# 从集合中删除项目时出错

C# 从集合中删除项目时出错,c#,C#,我试图从集合中删除一个元素,但最终出错 收集被修改;枚举操作不能执行 我正在努力实现什么? 检查报告集合中的学生列表计数 如果计数=1 然后检查学生属性的null或empty 如果全部为true,则从报告集合中删除学生 这是我的密码 public void Create(StudentReport report) { ICollection<StudentReportDetails> student= report.Students; if (student.Cou

我试图从集合中删除一个元素,但最终出错

收集被修改;枚举操作不能执行

我正在努力实现什么?

检查报告集合中的学生列表计数

如果计数=1 然后检查学生属性的null或empty

如果全部为true,则从报告集合中删除学生

这是我的密码

public void Create(StudentReport report)
{

    ICollection<StudentReportDetails> student= report.Students;
    if (student.Count == 1) 

    {
        foreach (StudentReportDetails Studetails in student)
        {

            if (String.IsNullOrEmpty(Studetails.StudentNumber)&& String.IsNullOrEmpty(Studetails.Description) && String.IsNullOrEmpty(Studetails.Summary))
            {
                report.Students.Remove(Studetails);
            }

        }
    }
}
public void创建(StudentReport报告)
{
i收集学生=报告。学生;
如果(student.Count==1)
{
foreach(学生报告详细信息学生详细信息)
{
if(String.IsNullOrEmpty(Studetails.StudentNumber)和&String.IsNullOrEmpty(Studetails.Description)和&String.IsNullOrEmpty(Studetails.Summary))
{
报告。学生。移除(学生细节);
}
}
}
}

是。不能修改集合。Foreach循环将集合设置为只读。

如果要修改它,请将
Foreach
更改为
for
循环:

for (int i = 0; i < student.Count; i++)
{ 

        if (String.IsNullOrEmpty(Studetails.StudentNumber)&& String.IsNullOrEmpty(Studetails.Description) && String.IsNullOrEmpty(Studetails.Summary))
        {
            report.Students.Remove(Studetails);
        }

    }
for(int i=0;i
它是如何标记C和C的?!!使用
for
循环,而不是像上面所说的那样使用
foreach
,您不能删除基于枚举数的循环中的集合项。并且我们可以理解为什么,在当前项目中是以下方式。但是您删除了当前项,因此,没有以下内容。@Garg Ankit为什么这么苛刻。。错误是人的本性。谢谢你的编辑。谢谢你的信息。我没有意识到这一点。