web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve multiple One to Many joint

(0) ShareShare
ReportReport
Posted on by 10

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)
  • Glide Profile Picture
    10 on at

    Actually it's not the Guid that I need but another alternate key in the Dictionary so I can't just query the item entity alone

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    You can do this with a Query Expression and a Left Join

    QueryExpression query = new QueryExpression("bag");

    query.ColumnSet = new ColumnSet(true);

    // if necessary add criteria on the bag entity

    query.Criteria = new FilterExpression();

    query.Criteria.AddCondition(...);

    // Add linked entity

    LinkEntity le = query.AddLink("item", "bagid", "bagid", JoinOperator.LeftOuter);

    le.Columns = new ColumnSet(...);

    le.EntityAlias = "item";

    // Retrieve the results

    EntityCollection results = service.RetrieveMultiple(query);

    Hope this helps.

  • Glide Profile Picture
    10 on at

    Thank you for you answer.

    I'm getting a very explicit and precise "Generic SQL error" to which I can't find the issue.

    I didn't know we could link entities from outside like this though.

  • ashlega Profile Picture
    34,477 on at

    Hi,

     not sure you can actually retrieve the first item per bag.. You might probably use FirstOrDefault to retrieve only one item per bag in your loop:

    svc.RetrieveMultiple(itemQuery).FirstOrDefault

     And you could use that LinkEntity as Aric suggested, but that would not give you 1 item per bag - you'll get an intersect instead (which would have multiple rows per bag - by the number of items)

  • Glide Profile Picture
    10 on at

    Thanks @Alex,

    I know that this link will get the row duplicated but maybe I still can filter them with a GroupBy().Select(g => g.First).

    FirstOrDefault I don't have this available. Is this an extension ?

  • ashlega Profile Picture
    34,477 on at

    My mistake, FirstOrDefault would be on the entities collection:

    svc.RetrieveMultiple(itemQuery).Entities.FirstOrDefault();

    For the group by, I'm not sure it would work on the server. Linq is translated into FetchXml, and here is what Fetch has for the  aggregates:

    msdn.microsoft.com/.../gg309565.aspx

    But you can always query everything, use "ToList()", and, then, do the grouping

  • Glide Profile Picture
    10 on at

    Yes I think I'll do that, perf is not optimal but it'll do.

    (FirstOrDefault() is still not available in the Entities attribute)

    I looked over the fetchXML technique but it seems like a pain in the arse to create queries and maintain them.

  • ashlega Profile Picture
    34,477 on at

    For the FirstOrDefault, you might not have System.Linq namespaces added.

    Just add it to the "using"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans