C#无法将system.collections.generic.icollection <kmsentities.pmrunittypes>转换为KMSEntities.PMRUnitTypes

时间:2018-11-09 17:29:33

标签: c#

我正在对我们的页面之一进行故障排除,我发现了一个feachloop,其中嵌套了foreach循环,而不是嵌套它们,我打算将多线程的foreach循环并单独分解

List<int> buildings = new List<int>();
            List<int> wings = new List<int>();
            List<int> complexes = new List<int>();
            List<int> unittypes = new List<int>();
            int floors = 0;
            int unitcnt = 0;
            ICollection<PMRUnitTypes> units = new List<PMRUnitTypes>();
            List<PMRProjectConfig> UnityTypesObj = new List<PMRProjectConfig>();


            //for each config in the project, get the summay

            foreach (var c in prj.Configs)
            {
                //add the buildings
                if (c.PB_ID.HasValue && buildings.FirstOrDefault(b => b == c.PB_ID.Value) <= 0)
                    buildings.Add(c.PB_ID.Value);

                //add the complexs
                if (c.PC_ID.HasValue && complexes.FirstOrDefault(co => co == c.PC_ID.Value) <= 0)
                    complexes.Add(c.PC_ID.Value);

                //add the wings
                if (c.PW_ID.HasValue && wings.FirstOrDefault(w => w == c.PW_ID.Value) <= 0)
                    wings.Add(c.PW_ID.Value);

                //add the floors
                if (c.Floor_ID.HasValue)
                    floors++;

                UnityTypesObj.Add(c.UnitTypes);

                //add the unit type codes
                foreach (var ut in c.UnitTypes)
                {
                    if (unittypes.FirstOrDefault(utc => utc == ut.PUTC_ID) <= 0)
                        unittypes.Add(ut.PUTC_ID);
                }

                //get the units
                var dscnt = DataServiceLocator.RunStoreProcedureReturnDS("GetPMRUnitsCountFromConfig", 200, new SqlParameter[]{
                    new SqlParameter{ParameterName = "@PPC_ID", Value= c.PPC_ID}
                });

                foreach (DataRow r in dscnt.Tables[0].Rows)
                {
                    unitcnt += int.Parse(r["unitCount"].ToString());
                }
            };

这是我的代码,但在第二个foreach循环中,不是这样做,而是尝试将其添加到列表中,然后在完成第一个初始循环后遍历它,但是我遇到了ICollection的错误。我发现PMRUnittypes是配置的ICollection,但是有什么可能的方法可以做到这一点或更好地编写代码,从而加快代码的速度?

错误发生在UnityTypesObj.Add(c.UnitTypes);上,并且proj.configs来自数据库var cust = DataServiceLocator.GetCustomer_DAO()。GetCustomerByID(customerID,Context);

1 个答案:

答案 0 :(得分:1)

由于c.UnitTypes是一个集合,因此您需要改用AddRange:

UnityTypesObj.AddRange(c.UnitTypes);