在ms dynamics crm中获取相关记录

本文关键字:记录 获取 ms dynamics crm | 更新日期: 2023-09-27 18:17:48

您好,从ms crm 2013中的campaignlist_association中获取相关记录。尝试了很多不同的变化。

这是最后一个:

System.Guid campaignId = ((EntityReference)entity.Attributes["regardingobjectid"]).Id;

var list = (from c in EntityCon.CampaignSet
            join l in EntityCon.ListSet on c.campaignlist_association equals l.campaignlist_association
            where c.CampaignId == campaignId select c).First();

在ms dynamics crm中获取相关记录

错误信息

连接子句中一个表达式的类型不正确。调用"Join"时类型推断失败

表示与等于表达式一起使用的属性类型必须匹配,例如它们都是Int32Guid

确保类型l.campaignlist_association与类型c.campaignlist_association相同

我将使用如下代码来获取相关的实体记录。根据需要更改列集。

private EntityCollection GetAssociatedEntityItems(string relationshipName, string relatedEntityName, string entityName, Guid entityId)
    {
        EntityCollection result = null;
            QueryExpression query = new QueryExpression();
            query.EntityName = relatedEntityName;
            query.ColumnSet = new ColumnSet(false);
            Relationship relationship = new Relationship();
            relationship.SchemaName = relationshipName;
            relationship.PrimaryEntityRole = EntityRole.Referencing;
            RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
            relatedEntity.Add(relationship, query);
            RetrieveRequest request = new RetrieveRequest();
            request.RelatedEntitiesQuery = relatedEntity;
            request.ColumnSet = new ColumnSet(true);
            request.Target = new EntityReference
            {
                Id = entityId,
                LogicalName = entityName
            };
            RetrieveResponse response = (RetrieveResponse)serviceProxy.Execute(request);
            RelatedEntityCollection relatedEntityCollection = response.Entity.RelatedEntities;
            if (relatedEntityCollection.Count > 0)
            {
                if (relatedEntityCollection.Values.Count > 0)
                {
                    result = (EntityCollection)relatedEntityCollection.Values.ElementAt(0);
                }
            }
        return result;
    }