locked
how to add multiple EntityCollections to a Single EntityCollection in MS CRM 2011 Plugin RRS feed

  • Question

  • EntityCollection PartyList = new EntityCollection();
               EntityCollection partylistfrom = activityEnt.GetAttributeValue<EntityCollection>("to");
               EntityCollection partylistto = activityEnt.GetAttributeValue<EntityCollection>("from");
               PartyList.Entities.Add(partylistfrom);
               PartyList.Entities.Add(partylistto); 

    Error "The best overloaded method match for 'System.Collections.ObjectModel.Collection<Microsoft.Xrm.Sdk.Entity>.Add(Microsoft.Xrm.Sdk.Entity)' has some invalid arguments"

    I tried declaring partylistfrom & partylistto as Entities, but then i get the error that CRM cannot type case Entity to EntityCollection.

    Please let me know how to add multiple EntityCollections to a Single EntityCollection. Any help is highly appreciated.

    Thanks


    • Edited by CRM5Developer80 Thursday, October 20, 2011 2:00 PM Adding More details
    Thursday, October 20, 2011 1:54 PM

Answers

  • Doh!  My bad.  You can't enumerate the collection, you can only enumerate the entities in the collection:
    foreach (Entity f in ActivityEnt.GetAttributeValue<EntityCollection>("from").Entities)
    {
    PartyList.Entities.Add(f);
    }
    foreach (Entity t in ActivityEnt.GetAttributeValue<EntityCollection>("to").Entities)
    {
    PartyList.Entities.Add(t);
    }
    
    

    Thursday, October 20, 2011 3:35 PM

All replies

  • Have you tried adding the entities contained in your sub-collections to your parent collection individually?
    Thursday, October 20, 2011 2:01 PM
  • Thanks for your reply. Yes, I tried declaring partylistfrom & partylistto as Entities as below:

     EntityCollection PartyList = new EntityCollection();
               Entity partylistfrom = activityEnt.GetAttributeValue<Entity>("from");
               Entity partylistto = activityEnt.GetAttributeValue<Entity>("to");
               PartyList.Entities.Add(partylistfrom);
               PartyList.Entities.Add(partylistto);
    

    Then i get the error at runtime as, Unable to cast object of type 'Microsoft.Xrm.Sdk.EntityCollection' to type 'Microsoft.Xrm.Sdk.Entity'.

    Thursday, October 20, 2011 2:32 PM
  • I am assuming your PartyList, and activityEnt are both EntityCollections.  Thus I was thinking something more along the lines of:

    foreach (Entity a in activityEnt.Entities)
    {
    PartyList.Entities.Add(a);
    }
    

     

    -JayB
    Thursday, October 20, 2011 2:39 PM
  • No, ActivityEnt is a Phone call Activity and From and To are the party list fields in Phonce Call Entity. I am trying to add all the values in the From and To field to a single entity collection.

    Thanks.

    Thursday, October 20, 2011 3:08 PM
  • Ok, that makes sense.  Since PartyList can be mapped to EntityCollection modify slightly:

    foreach (Entity f in ActivityEnt.GetAttributeValue<EntityCollection>("from"))
    {
    PartyList.Entities.Add(f);
    }
    foreach (Entity t in ActivityEnt.GetAttributeValue<EntityCollection>("to"))
    {
    PartyList.Entities.Add(t);
    }
    

     


    • Edited by JBlaeske Thursday, October 20, 2011 3:23 PM
    Thursday, October 20, 2011 3:22 PM
  •  EntityCollection PartyList = new EntityCollection();
               foreach (Entity f in activityEnt.GetAttributeValue<EntityCollection>("from"))
               {
                   PartyList.Entities.Add(f);
               }
               foreach (Entity t in activityEnt.GetAttributeValue<EntityCollection>("to"))
               {
                   PartyList.Entities.Add(t);
               }
    

    Sorry, getting the Error : foreach statement cannot operate on variables of type 'Microsoft.Xrm.Sdk.EntityCollection' because 'Microsoft.Xrm.Sdk.EntityCollection' does not contain a public definition for 'GetEnumerator' 

    Thanks

    Thursday, October 20, 2011 3:29 PM
  • Doh!  My bad.  You can't enumerate the collection, you can only enumerate the entities in the collection:
    foreach (Entity f in ActivityEnt.GetAttributeValue<EntityCollection>("from").Entities)
    {
    PartyList.Entities.Add(f);
    }
    foreach (Entity t in ActivityEnt.GetAttributeValue<EntityCollection>("to").Entities)
    {
    PartyList.Entities.Add(t);
    }
    
    

    Thursday, October 20, 2011 3:35 PM
  • Great! Thanks a lot. Just got delayed a little to handle the code to work in Pre-Images when updating.
    Thursday, October 20, 2011 4:41 PM
  • I have similar issue.

     

    But I changed the partlist in a plugin record in Pre-Transaction. but commit of my change into crm doesn't work. I also tried the Pre-Validation Stage and it's not working also.

     

    Any idea why it's not working?

    Tuesday, October 25, 2011 1:57 PM
  • Can you post the code and the error (if there is one)?
    Tuesday, October 25, 2011 2:08 PM
  • I had it registered in the Post-Operation inorder to work.This could be your issue.

    Thursday, October 27, 2011 6:37 PM