c# cannot convert system.collections.generic.icollection to KMSEntities.PMRUnitTypes
up vote
1
down vote
favorite
I was troubleshooting one of our pages and I found a feachloop that had nested foreach loops in it and instead of nesting them I was going to multithread the foreach loops and break them out individually
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 SqlParameterParameterName = "@PPC_ID", Value= c.PPC_ID
);
foreach (DataRow r in dscnt.Tables[0].Rows)
unitcnt += int.Parse(r["unitCount"].ToString());
;
here is my code but on the second foreach loop instead of doing that I was trying to add it to list then loop through it after this initial one is done but I am getting the error above with ICollection. I found out that PMRUnittypes is a ICollection of the config but is there any possible way to do this or write this better which would speed up the code?
the error is on UnityTypesObj.Add(c.UnitTypes);
and proj.configs come from the database var cust =
DataServiceLocator.GetCustomer_DAO().GetCustomerByID(customerID, Context);
c#
|
show 3 more comments
up vote
1
down vote
favorite
I was troubleshooting one of our pages and I found a feachloop that had nested foreach loops in it and instead of nesting them I was going to multithread the foreach loops and break them out individually
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 SqlParameterParameterName = "@PPC_ID", Value= c.PPC_ID
);
foreach (DataRow r in dscnt.Tables[0].Rows)
unitcnt += int.Parse(r["unitCount"].ToString());
;
here is my code but on the second foreach loop instead of doing that I was trying to add it to list then loop through it after this initial one is done but I am getting the error above with ICollection. I found out that PMRUnittypes is a ICollection of the config but is there any possible way to do this or write this better which would speed up the code?
the error is on UnityTypesObj.Add(c.UnitTypes);
and proj.configs come from the database var cust =
DataServiceLocator.GetCustomer_DAO().GetCustomerByID(customerID, Context);
c#
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
1
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
1
Somewhere here, in this code, you trying to stickcollection<a>
in toa
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear
– T.S.
Nov 9 at 18:27
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I was troubleshooting one of our pages and I found a feachloop that had nested foreach loops in it and instead of nesting them I was going to multithread the foreach loops and break them out individually
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 SqlParameterParameterName = "@PPC_ID", Value= c.PPC_ID
);
foreach (DataRow r in dscnt.Tables[0].Rows)
unitcnt += int.Parse(r["unitCount"].ToString());
;
here is my code but on the second foreach loop instead of doing that I was trying to add it to list then loop through it after this initial one is done but I am getting the error above with ICollection. I found out that PMRUnittypes is a ICollection of the config but is there any possible way to do this or write this better which would speed up the code?
the error is on UnityTypesObj.Add(c.UnitTypes);
and proj.configs come from the database var cust =
DataServiceLocator.GetCustomer_DAO().GetCustomerByID(customerID, Context);
c#
I was troubleshooting one of our pages and I found a feachloop that had nested foreach loops in it and instead of nesting them I was going to multithread the foreach loops and break them out individually
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 SqlParameterParameterName = "@PPC_ID", Value= c.PPC_ID
);
foreach (DataRow r in dscnt.Tables[0].Rows)
unitcnt += int.Parse(r["unitCount"].ToString());
;
here is my code but on the second foreach loop instead of doing that I was trying to add it to list then loop through it after this initial one is done but I am getting the error above with ICollection. I found out that PMRUnittypes is a ICollection of the config but is there any possible way to do this or write this better which would speed up the code?
the error is on UnityTypesObj.Add(c.UnitTypes);
and proj.configs come from the database var cust =
DataServiceLocator.GetCustomer_DAO().GetCustomerByID(customerID, Context);
c#
c#
edited Nov 9 at 19:22
asked Nov 9 at 17:29
Josh Deshazer
285
285
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
1
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
1
Somewhere here, in this code, you trying to stickcollection<a>
in toa
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear
– T.S.
Nov 9 at 18:27
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21
|
show 3 more comments
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
1
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
1
Somewhere here, in this code, you trying to stickcollection<a>
in toa
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear
– T.S.
Nov 9 at 18:27
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
1
1
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
1
1
Somewhere here, in this code, you trying to stick
collection<a>
in to a
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear– T.S.
Nov 9 at 18:27
Somewhere here, in this code, you trying to stick
collection<a>
in to a
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear– T.S.
Nov 9 at 18:27
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Since c.UnitTypes
is a collection, you need to use AddRange instead:
UnityTypesObj.AddRange(c.UnitTypes);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Since c.UnitTypes
is a collection, you need to use AddRange instead:
UnityTypesObj.AddRange(c.UnitTypes);
add a comment |
up vote
1
down vote
accepted
Since c.UnitTypes
is a collection, you need to use AddRange instead:
UnityTypesObj.AddRange(c.UnitTypes);
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Since c.UnitTypes
is a collection, you need to use AddRange instead:
UnityTypesObj.AddRange(c.UnitTypes);
Since c.UnitTypes
is a collection, you need to use AddRange instead:
UnityTypesObj.AddRange(c.UnitTypes);
answered Nov 9 at 20:15
LarsTech
69.1k12101149
69.1k12101149
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230644%2fc-sharp-cannot-convert-system-collections-generic-icollectionkmsentities-pmruni%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If the data size per PPC_ID in the call against the db isn't that big, consider fetching all data from db once, before the loop, then filter the dataset from db in memory in the loop.
– Marcus Höglund
Nov 9 at 17:40
1
It always helps to tell us which line is throwing the error.
– LarsTech
Nov 9 at 17:42
1
Somewhere here, in this code, you trying to stick
collection<a>
in toa
. The error tells you that. Posting definitions, error lines, etc would help. As is, this is not clear– T.S.
Nov 9 at 18:27
unittypesobj.add(c.unittypes); is throwing the error
– Josh Deshazer
Nov 9 at 19:20
that comes from getting the prj data from the db var prj = DataServiceLocator.GetPMR_Project_DAO().GetPMRprojectById(id, Context);
– Josh Deshazer
Nov 9 at 19:21