0

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?

gymcode
  • 4,431
  • 15
  • 72
  • 128

2 Answers2

1

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.

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
1

Building on what Guido said, it is also possible to create a new EntityCollection with the results:

var matchedRecords = allRecords.Entities.Where(x => x.GetAttributeValue<EntityReference>("ccc_batchId").Id == batchId).ToList();    
var matchedCollection = new EntityCollection(matchedRecords);
Aron
  • 3,877
  • 3
  • 14
  • 21