收集被修改;在windows窗体中使用treenode和foreach循环以及linq。 [英] Collection was modified; using treenode and foreach loop and linq in windows form.

查看:69
本文介绍了收集被修改;在windows窗体中使用treenode和foreach循环以及linq。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每当我运行我的应用程序时,我都有下面的代码,它给我一个错误,说收集已被修改。



Hi,
I have the code below whenever I run my application it gives me an error saying "collection was modified."

MyTreeNode Node;

            //-----SYSTEM-----
            //Gets the SYSTEMS into the variable currentSystems
            var currentSystems = DAObject.fetch("SELECT * FROM TSystem").AsEnumerable();
            if (currentSystems.Count() > 0)
                foreach (DataRow systemRow in currentSystems)
                {
                    Node = new MyTreeNode(
                        systemRow["strSystemName"].ToString(),
                        DAObject.GetLinage(systemRow["strFormIdName"].ToString()).ToString(),
                        systemRow["strFormIdName"].ToString()
                        );

                    Node.ForeColor = Color.Green;
                    treeView1.Nodes.Add(Node);

                    //-----SUBSYSTEMS-----
                    //Gets the system SUBSYSTEMS into the variable currentSystemSubSystems
                    var currentSystemSubSystems =
                                from subSystem in (DAObject.fetch("SELECT * FROM TSubSystem").AsEnumerable())
                                where subSystem["nSystemId"] == systemRow["nSystemId"]
                                select subSystem;
                    if (currentSystemSubSystems.Count() > 0)
                        foreach (DataRow subSystemRow in currentSystemSubSystems)
                        {
                            Node = new MyTreeNode(
                                subSystemRow["strSubsystemName"].ToString(),
                                DAObject.GetLinage(subSystemRow["strSubFormID"].ToString()).ToString(),
                                subSystemRow["strSubFormID"].ToString()
                                );

                            Node.ForeColor = Color.Green;
                            treeView1.Nodes.Add(Node);

                            //-----MENUS-----
                            //Gets the Subsystem MENUS into the variable currentSubSystemMenus
                            var currentSubSystemMenus =
                                        from Menu in (DAObject.fetch("SELECT * FROM TMenu").AsEnumerable())
                                        where Menu["nSubsytemId"] == subSystemRow["nSubsytemId"]
                                        select Menu;
                            if (currentSubSystemMenus.Count() > 0)
                                foreach (DataRow menuRow in currentSubSystemMenus) //First time error comes up at this line, when I continue it occurs on other loops 
                                {
                                    Node = new MyTreeNode(
                                        subSystemRow["strMenuname"].ToString(),
                                        DAObject.GetLinage(subSystemRow["strMenuFormID"].ToString()).ToString(),
                                        subSystemRow["strMenuFormID"].ToString()
                                        );

                                    Node.ForeColor = Color.Green;
                                    treeView1.Nodes.Add(Node);


                                    //-----SUBMENUS-----
                                    //Gets the Menu SUBMENUS into the variable currentMenuSubMenus
                                    var currentMenuSubMenus =
                                        from subMenu in (DAObject.fetch("SELECT * FROM TSubMenu").AsEnumerable())
                                        where subMenu["nMenuID"] == menuRow["nMenuID"]
                                        select subMenu;
                                    if (currentMenuSubMenus.Count() > 0)
                                        foreach (DataRow subMenuRow in currentMenuSubMenus)
                                        {
                                            Node = new MyTreeNode(
                                                subSystemRow["strSubMenuName"].ToString(),
                                                DAObject.GetLinage(subSystemRow["strSubMenuFormID"].ToString()).ToString(),
                                                subSystemRow["strSubMenuFormID"].ToString()
                                                );

                                            Node.ForeColor = Color.Green;
                                            treeView1.Nodes.Add(Node);

                                        }
                                    Menu_count += 1;
                                }
                            SubSystem_count += 1;
                        }
                    System_count += 1;

                }





我正在读它但我不明白这是什么问题,

数据表和数据行被正确填充和估价。



e1:

搜索后我看到了在菜单foreach循环的第一次迭代之后,看起来像dataTable被删除了。我不知道为什么。



我的尝试:



我还在努力,但我找不到任何解决方案。



I'm reading it but I con't see what the problem is,
the data tables and data rows are correctly filled and valued.

e1:
After searching for it I saw that looks like the dataTable is removed after the first iteration of the MENU foreach loop. I don't know why.

What I have tried:

I'm still working on it, but yet I couldn't find any solution's for it.

推荐答案

我强烈建议你阅读:枚举在C#中更改的集合Magnus Montin [ ^ ]

简而言之:你不能在 forach 循环中修改集合。
I'd strongly recommend to read this: Enumerating collections that change in C# | Magnus Montin[^]
In a short: you cannot modify collection inside a forach loop.


这篇关于收集被修改;在windows窗体中使用treenode和foreach循环以及linq。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆