【问题标题】:Retrieving n to n records检索 n 到 n 条记录
【发布时间】:2011-12-21 16:10:14
【问题描述】:

(不,又不是法国女孩!!!)

所以...我有两个具有 n-n 关系的实体:“new_produit”和“new_lignecontrat”。 我需要将最后一个“new_lignecontrat”记录的“new_produit”记录复制到新创建的“new_lignecontrat”中。

插件在为 new_lignecontrat 创建时触发。

到目前为止,我已经写了这个,但我不太确定复制“new_produit”记录要遵循的步骤......

            else if (modeleContrat.Id.Equals (ContratForfaitaire))
            {

                FetchExpression fetch = new FetchExpression(@"
                    <fetch distinct='false' mapping='logical'>
                      <entity name='new_contrats'><link-entity name='" + context.PrimaryEntityName + "' alias='nombreligne' from='new_contratsid' to='new_contratsid'><filter type='and'><condition attribute='new_contratsid' value='" + contrats.Id + "' operator='eq'></condition></filter></link-entity></entity></fetch>");
                EntityCollection lines = service.RetrieveMultiple(fetch);

                if (lines.Entities.Any())
                {
                    var last = lines.Entities.Last();

                    if (last.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
                    {

                        QueryExpression query = new QueryExpression();
                        query.EntityName = "new_produit";
                        query.ColumnSet = new ColumnSet("new_produitid");
                        Relationship relationship = new Relationship();

                        // name of relationship between team & systemuser
                        relationship.SchemaName = "new_new_lignecontrat_new_produit";
                        RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
                        relatedEntity.Add(relationship, query);
                        RetrieveRequest request = new RetrieveRequest();
                        request.RelatedEntitiesQuery = relatedEntity;
                        request.ColumnSet = new ColumnSet("new_lignecontratid");
                        request.Target = new EntityReference
                        {
                            Id = last.Id,
                            LogicalName = last.LogicalName

                        };

                        RetrieveResponse response = (RetrieveResponse)service.Execute(request);



                        if (((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("new_new_lignecontrat_new_produit")) && ((DataCollection<Relationship, EntityCollection>)(((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("new_new_lignecontrat_new_produit")].Entities.Count > 0)
                        {
                            response.Entity.Attributes.Remove("new_produitid");
                            response["new_lignecontratid"] = new EntityReference(target.LogicalName, target.Id);

【问题讨论】:

  • 身为法国女孩不是不接受答案的借口;)
  • 哎呀...我完全忘了接受答案...对不起>
  • 您已经解决了问题,无需道歉 :)

标签: c# plugins dynamics-crm-2011


【解决方案1】:

我相信我回答这个问题的部分原因是回复one of your previous questions,所以让我们看看我这次的回答是否更好。

这个问题的三个步骤,如果我理解问题正确的话,就是先获取在创建当前new_lignecontrat记录之前创建的最后一个new_lignecontrat记录,然后获取所有关联的new_produit记录最后一个 new_lignecontrat,最后是 Associate 每个 new_produit 记录和新的 new_lignecontrat 记录。

  1. 对于此查询,您可以使用 Dynamics 中三种受支持的查询方法(FetchXml、QueryExpression 和 Linq)中的任何一种。我更喜欢 Linq,因为 .First() 方法是一种执行高效 TOP 查询的简单方法(以获取您最后创建的 new_lignecontrat 记录),但您也可以使用 FetchXml 和分页 cookie 来完成相同的操作(不确定关于 QueryExpression)。

  2. 您可以使用 N:N 表和步骤 1 中检索到的 ID 获取链接的 new_produit 记录。

  3. 一旦您有了新的new_lignecontrat 的ID 和最后关联的new_produit 记录的ID,就可以针对这些记录及其关系执行IOrganizationService 的Associate 方法。下面是处理这三个步骤的模型。

using (OrganizationServiceContext osc = new OrganizationServiceContext(service))
{
    //assumes early binding, but this can be modified for late binding as well
    Guid ligneContratID = (from lc in osc.CreateQuery<new_ligneContrat>()
                           where lc.CreatedOn < (DateTime)targetEntity.Attributes["CreatedOn"] //look at all new_ligneContrat records created before the newly created record
                           orderby lc.CreatedOn descending //sort newest to oldest
                           select lc.new_ligneContratID)
                           .First(); //get the newest record

    var produits = from lcp in osc.CreateQuery<new_new_lignecontrat_new_produit>()  //use your N:N relationship/table as your linking table
                   join p in osc.CreateQuery<new_produit>() on lcp.new_produitId equals p.new_produitId
                   where lcp.new_lignecontratId = ligneContratID //use the previous query result in your N:N lookup
                   select p.new_produitId;                        

    EntityReferenceCollection erc = new EntityReferenceCollection();
    foreach (var e in produits)
    {
        erc.Add(new EntityReference("new_produit", e));
    }

    service.Associate("new_lignecontrat", targetEntity.Id, new Relationship("new_new_lignecontrat_new_produit", erc);
}

【讨论】:

  • 嗨,彼得,谢谢。您之前的回答还不错,很有帮助。有人告诉我,查询表达式是实现我们想要的最佳方式,但我发现 Linq 方法更适合编码。
猜你喜欢
  • 2013-11-23
  • 1970-01-01
  • 2013-09-30
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多