Reputation: 871
I'm trying to retrieve data from crm in a .net application, using the SDK. I've managed to do simple queries to retrieve lists, but I would now like to get the related entities with items, rather than the ids.
I have tried things like
QueryExpression query = new QueryExpression
{
EntityName = "opportunity",
....
LinkEntity linkEntityAccount = new LinkEntity()
{
LinkFromEntityName = "opportunity",
LinkFromAttributeName = "opportunityid",
LinkToEntityName = "serviceappointment",
LinkToAttributeName = "regardingobjectid",
JoinOperator = JoinOperator.Inner,
Columns = new ColumnSet(new string[] { "scheduledstart", "scheduledend" }),
EntityAlias = "service"
};
query.LinkEntities.Add(linkEntityAccount);
(This will return a collection of entities from the opportunity table)
However the LinkedEntities just put the two columns in the returns entities.
What i would like is (say for this example) is a entity.serviceappointment
to be the the entity containing the service appointment entity/data. Instead of in entity there being fields such as service.scheduledstart
and service.scheduledend
I have looked at the Relationship
and RelationshipQueryCollection
things in the SDK but i have been unable to setup a query that will do the query, without first getting the opportunity
entities. But it looks like that maybe what I need? I'm not sure.
Is this even possible? Or should I just continue to query entities individually?
Thanks
Upvotes: 0
Views: 1567
Reputation: 5650
There's no automatic way to get the entire linked entity data (as an Entity
object) that I know of (that's not to say it's impossible, mind you).
But I think it'd be a lot easier to just query the data you need in another request.
Upvotes: 0
Reputation: 7948
In the QueryExpression
the LinkEntity
represents a join. That's why the fields of the joined table are in the Entity
row. They can be distinguished from the 'real' entity attributes by the fact that their names are prefixed (including a dot) and their values are wrapped in an AliasedValue
object.
It is possible to unwrap them and create strong typed Entity
objects, but you will need to write the code yourself.
Alternatively you can consider a few other options:
serviceappointment
records and join the opportunity
records.opportunity
records one by one using the RetrieveRequest
and include the query for the related service appointments in the request. (See also this discussion on StackOverflow.)OrganizationResponse
.Upvotes: 1