在mscorlib.dll中发生StackOverflowException,列表<>

时间:2017-05-25 21:06:55

标签: c# entity-framework list

因此,当我尝试加载带有数据的组合框(一个填充了数据库中数据的列表)时,我在mscorlib.dll中发生了StackOverflowException。我在表单构造函数中使用它:

dbATMT是我的实体框架数据库;

List<Team> teams = (from t in dbATMT.TeamSet
                          select t).ToList<Team>();

combobox1.Items.AddRange(teams.ToArray());
combobox2.Items.AddRange(teams.ToArray());

然后我有了这个,以避免我的组合框选择相同的值:

private void FillList()
    {
        List<Team> teams = (from t in dbATMT.TeamSet
                              select t).ToList<Team>();`(I get my error here)`

        Team teamSelected= (Team)combobox1.SelectedItem;

        List<Team> teams2 = new List<Team>(teams.Count);

        foreach (Team tin equipas)
        {
            teams2.Add(equipa);
        }

        teams2.Remove(teamSelected);

        combobox2.Items.Clear();
        combobox2.Items.AddRange(teams2.ToArray());
        combobox2.SelectedItem = teams2.ToArray()[0];

    }

最后,我调用了combobox1_SelectedIndexChanged上的FillList()和combobox2_SelectedIndexChanged

1 个答案:

答案 0 :(得分:2)

FillList的最后一行会更改所选索引。选定的索引更改时将调用sales$LatestSaleDate[as.character(sales$LatestSaleDate) == "NA"] <- as.Date("1900-1-1") 。因此,一个无限循环一直运行,直到你的堆栈空间用完(StackOverflowException)。

类似问题的建议解决方案Modifying ComboBox SelectedIndex Without Triggering Event in C#