Search code examples
linqdynamics-crmentityentitycollection

Get All Entities From EntityCollection Where Values Matches X


I would like to get all matched Entities from an EntityCollection by a value, but my current statement only allow return of Entity, and not EntityCollection.

//only allow return 1 entity
var matchedRecords = allRecords.Entities.Where
                                (x => x.GetAttributeValue<EntityReference>
                                                         ("ccc_batchId").Id == batchId);

May I know how can I tweak the above query?


Solution

  • EntityCollection is just a construct to store more than one Entity.

    I know it's not ideal but you can always convert the allRecords.Entities inside a List of Entity and do your LINQ query against it.

    Your code is probably returning an IEnumerable of Entity and not a single Entity (for example in the end of your query you can put a .ToList() to get a List of Entity.