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);










share|improve this question























  • 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 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










  • 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














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);










share|improve this question























  • 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 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










  • 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












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);










share|improve this question















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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






  • 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 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










  • 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












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);





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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);





    share|improve this answer
























      up vote
      1
      down vote



      accepted










      Since c.UnitTypes is a collection, you need to use AddRange instead:



      UnityTypesObj.AddRange(c.UnitTypes);





      share|improve this answer






















        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);





        share|improve this answer












        Since c.UnitTypes is a collection, you need to use AddRange instead:



        UnityTypesObj.AddRange(c.UnitTypes);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 20:15









        LarsTech

        69.1k12101149




        69.1k12101149



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Darth Vader #20

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Ondo