指数超出范围.必须是非负误差

本文关键字:是非 误差 范围 指数 | 更新日期: 2023-09-27 18:36:48

我不断得到一个超出范围的索引异常。不得为非负数且小于集合的大小。

问题是当我检查toRemove计数低于受访者时。那么这个错误是如何发生的呢?如果 5 个在respondentstoRemove只有 3 个,那么此错误是如何发生的?

var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude);
// iterate through the respondents. If search query not like results throw the result away.
List<int> toRemove = new List<int>();
for (int i = 0; i < respondents.Count; i++)
{
    if (!respondents[i].EmailAddresses.Any())
        toRemove.Add(i);
    else
    {
        bool checkSingleEmail = false;
        bool checkAllEmails = false;
        for (int j = 0; j < respondents[i].EmailAddresses.Count; j++)
        {
            checkSingleEmail = respondents[i].EmailAddresses[j].Address.ToString().Contains(query);
            if (checkSingleEmail == true)
                checkAllEmails = true;
            if (respondents[i].EmailAddresses.Count == 1 && j == 0 && checkAllEmails == false)
                toRemove.Add(i);
            else if (checkAllEmails == false && j+1 == respondents[i].EmailAddresses.Count)
                toRemove.Add(i);
        }
    }
}
foreach (var respRemove in toRemove)
{
    respondents.RemoveAt(respRemove);
}

指数超出范围.必须是非负误差

如果你想使用索引,你可以这样写:

foreach (var respRemove in toRemove.OrderByDesc(r => r).ToList())
{
    respondents.RemoveAt(respRemove);
}

发生异常的原因是,您从列表中删除了项目,然后假定其他项目仍位于同一索引中。请尝试改为保留对项目的引用:

var respondents = RespondentRepository.GetRespondents(UserSession, fieldsToInclude);
                // iterate through the respondents. If search query not like results throw the result away.
                List<Respondent> toRemove = new List<Respondent>();
                for (int i = 0; i < respondents.Count; i++)
                {
                    if (!respondents[i].EmailAddresses.Any())
                        toRemove.Add(respondents[i]);
                    else
                    {
                        bool checkSingleEmail = false;
                        bool checkAllEmails = false;
                        for (int j = 0; j < respondents[i].EmailAddresses.Count; j++)
                        {
                            checkSingleEmail = respondents[i].EmailAddresses[j].Address.ToString().Contains(query);
                            if (checkSingleEmail == true)
                                checkAllEmails = true;
                            if (respondents[i].EmailAddresses.Count == 1 && j == 0 && checkAllEmails == false)
                                toRemove.Add(respondents[i]);
                            else if (checkAllEmails == false && j+1 == respondents[i].EmailAddresses.Count)
                                toRemove.Add(respondents[i]);
                        }
                    }
                }
                foreach (var respRemove in toRemove)
                {
                    respondents.Remove(respRemove);
                }

例如,最后一项可能一开始就在索引 4 处。但是,如果删除索引 3,则它现在位于索引 3 处,索引 4 不存在。

您需要更改:

respondents.RemoveAt(respRemove);

respondents.Remove(respRemove);

这是由于RemoveAt()绑定到索引,并且由于该项已被删除,命令将变为无效。

在删除之前颠倒 toRemove 的顺序。 然后,在每次删除后保留其余项目的索引。

toRemove.Reverse();
foreach (int respRemove in toRemove)
{
    respondents.RemoveAt(respRemove);
}

另一件需要注意的事情是,如果我被添加不止一次:

    for (int j = 0; j < respondents[i].EmailAddresses.Count; j++)
    {
       ...
            toRemove.Add(i);

不要让我多次被添加到删除。