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



我在对我们的一个页面进行故障排除时,发现了一个在其中嵌套了foreach循环的feachloop,我将对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.config来自数据库var cust =DataServiceLocator.GetCustomer_DAO((.GetCustomerByID(customerID,Context(;

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

UnityTypesObj.AddRange(c.UnitTypes);

最新更新