I'm using Query Expression to retrieve related records through N:N relationship between 2 entities (User and Engagement)
But when I proceed with return data from query. the method entity.Attributes["systemuserid"] always return blank although it has data.
Here is my code:
QueryExpression query = new QueryExpression(CrmAttributes.EntityName.User);
query.ColumnSet = new ColumnSet(CrmAttributes.SystemUserAttributes.UserId);
LinkEntity linkEntityFrom = new LinkEntity(CrmAttributes.EntityName.User,
CrmAttributes.EntityRelationships.EngagementToSystemUser,
CrmAttributes.SystemUserAttributes.UserId, CrmAttributes.SystemUserAttributes.UserId,
JoinOperator.Inner);
LinkEntity linkEntityTo = new LinkEntity(CrmAttributes.EntityRelationships.EngagementToSystemUser,
CrmAttributes.EntityName.Engagement,
CrmAttributes.EngagementAttributes.EngagementId, CrmAttributes.EngagementAttributes.EngagementId,
JoinOperator.Inner);
linkEntityFrom.LinkEntities.Add(linkEntityTo);
query.LinkEntities.Add(linkEntityFrom);
linkEntityFrom.LinkCriteria = new FilterExpression();
linkEntityFrom.LinkCriteria.AddCondition(
new ConditionExpression(CrmAttributes.EngagementAttributes.EngagementId, ConditionOperator.Equal, id));
var results = service.RetrieveMultiple(query);
foreach (var entity in results.Entities)
{
if(entity.Attributes.Contains(CrmAttributes.SystemUserAttributes.UserId)) {
Guid userGuid = entity.Attributes[CrmAttributes.SystemUserAttributes.UserId] //systemuserid
tracer.Trace("Guid: " + userGuid); // Always blank
}
}
But when I use KeyValuePair to get data. The data is exactly what I want
foreach (var entity in results.Entities)
{
Guid userGuid = new Guid();
foreach (KeyValuePair<string, object> attribute in entity.Attributes)
{
if (attribute.Key == CrmAttributes.SystemUserAttributes.UserId)
{
userGuid = (Guid)attribute.Value;
tracer.Trace("Guid: " + userGuid); //Return expected data
}
}
}