0

给定实体:团队、用户。它们之间的关系是 N:N。

问题:如何找到属于指定团队的用户(具有给定的 id 列表)。

PS。我找到了如何处理单个团队,但不知道如何处理团队列表?

var team_id = ...
QueryExpression query = new QueryExpression("user");

// setting up relation between teams and users
Relationship rel = new Relationship();
rel.SchemaName = "new_teams_users";

RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
relatedEntity.Add(rel, query);

RetrieveRequest request = new RetrieveRequest();
request.RelatedEntitiesQuery = relatedEntity;
request.ColumnSet = new ColumnSet(new string[] {"id"});

request.Target = new EntityReference { Id = team_id, LogicalName = "new_team" };

// Results: List of users by team id.
RetrieveResponse response = (RetrieveResponse)CrmService.Execute(request);
4

2 回答 2

3

建立在相交实体上的 QueryExpression 将为您提供帮助。例如,我使用了产品和竞争对手 N:N 关系

QueryExpression qe = new QueryExpression()
{
    EntityName = "competitorproduct",  //this name can be get from N:N rel properties (Relationship form, Relationship Entity Name field)
    ColumnSet = new ColumnSet(true),
};

qe.Criteria.AddCondition(
    "competitorid", 
    ConditionOperator.In, 
    new object[] { "GUID1", "GUID2"});

//Below is optional - if you need some details of entity, add LinkEntity object. This example adds all fields from product entity
LinkEntity lePorduct = new LinkEntity("competitorproduct", "product", "productid", "productid", JoinOperator.Inner);
lePorduct.Columns = new ColumnSet(true);
qe.LinkEntities.Add(lePorduct);
于 2013-10-17T22:07:15.170 回答
0

您将使您的主要实体成为交叉实体,因此在您的示例中它将是“TeamMembership”,然后将针对属性“SystemUserId”设置标准。

要获取有关团队的更多信息,您需要将团队实体作为链接实体添加到您的查询中,如下所示

 LinkEntity TeamLink = new LinkEntity();
            TeamLink .EntityAlias = "TeamLink ";
            TeamLink .JoinOperator = JoinOperator.Inner;
            TeamLink .LinkFromEntityName = "teammembership";
            TeamLink .LinkFromAttributeName = "teamid";
            TeamLink .LinkToEntityName = "team";
            TeamLink .LinkToAttributeName = "teamid";

然后,您可以带回您想要的任何列并获取数据。

于 2013-10-16T15:14:35.743 回答