Imagine 2 entities: Bag <- (One to Many) -> Item
I need to retrieve all the bags with the first item I find in each:
bag1, item1
bag2, item42
bag42, item100
Let's get the data:
var query = new QueryExpression("bag");
var result = svc.RetrieveMultiple(query);
var bags = new Dictionary<Guid, Guid>();
foreach (var entity in result.Entities)
{
var itemQuery = new QueryExpression("item");
itemQuery.Criteria.AddCondition(new ConditionExpression("bag", ConditionOperator.Equal, entity.Id));
var itemResult = svc.RetrieveMultiple(itemQuery);
bags[entity.Id] = itemResult.Entities[0].Id;
}
This works but of course it's not optimized since it executes as much queries as there are bags.
Isn't there a way to bring the whole thing in one query and maybe use a LinQ GroupBy or something like that to get the first item for each bag ?
Or maybe I could use the Many to One relationship (from item to bag) but I don't know how with query linked entities.
*This post is locked for comments
I have the same question (0)