locked
unable to cast object of type Entity RRS feed

  • Question

  • I have casting issues with these lines: 

    Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'Entities.Incident'.

    Incident incident = (Incident)context.InputParameters["Target"]

    Casting issue on this line too:

    DataCollection<Entity> casePayment = localContext.OrganizationService.RetrieveMultiple(q).Entities;

    Any suggestions for these?

    Wednesday, March 14, 2012 11:40 PM

Answers

  • The InputParameters collection contains generic classes such as Entity and knows nothing of your early-bound classes.  To convert the generic Entity to its early-bound equivalent, you can use the following:

    Entity entity = (Entity)context.InputParameters["Target"];
    
    Incident incident = entity.ToEntity<Incident>();
    
    Not sure about the latter issue.  In of itself, it appears valid, but how are localContext and q defined?


    --pogo (pat) @ pogo69.wordpress.com

    • Proposed as answer by 王红福 Thursday, March 15, 2012 1:47 AM
    • Marked as answer by Linq2011 Thursday, March 15, 2012 2:18 PM
    Thursday, March 15, 2012 12:29 AM
  • DataCollection<Entity> casePayment = localContext.OrganizationService.RetrieveMultiple(q).Entities;

    may need to be changed to

    EntityCollection casePayment = localContext.OrganizationService.RetrieveMultiple(q).Entities;

    RetrieveMultiple returns EntityCollection.

    You can then do

    foreach (Entity x in EntityCollection) to traverse through the results.

    HTH

    Sam


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Proposed as answer by Sam - Inogic Thursday, March 15, 2012 3:13 AM
    • Marked as answer by Linq2011 Thursday, March 15, 2012 2:18 PM
    Thursday, March 15, 2012 3:13 AM

All replies

  • The InputParameters collection contains generic classes such as Entity and knows nothing of your early-bound classes.  To convert the generic Entity to its early-bound equivalent, you can use the following:

    Entity entity = (Entity)context.InputParameters["Target"];
    
    Incident incident = entity.ToEntity<Incident>();
    
    Not sure about the latter issue.  In of itself, it appears valid, but how are localContext and q defined?


    --pogo (pat) @ pogo69.wordpress.com

    • Proposed as answer by 王红福 Thursday, March 15, 2012 1:47 AM
    • Marked as answer by Linq2011 Thursday, March 15, 2012 2:18 PM
    Thursday, March 15, 2012 12:29 AM
  • DataCollection<Entity> casePayment = localContext.OrganizationService.RetrieveMultiple(q).Entities;

    may need to be changed to

    EntityCollection casePayment = localContext.OrganizationService.RetrieveMultiple(q).Entities;

    RetrieveMultiple returns EntityCollection.

    You can then do

    foreach (Entity x in EntityCollection) to traverse through the results.

    HTH

    Sam


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Proposed as answer by Sam - Inogic Thursday, March 15, 2012 3:13 AM
    • Marked as answer by Linq2011 Thursday, March 15, 2012 2:18 PM
    Thursday, March 15, 2012 3:13 AM