2011-12-21 80 views
1

(不,不是法国姑娘再次!!!)检索N到N记录

所以...我有两个实体与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); 
+3

作为一个法国女孩是没有理由不接受的答案;) – 2011-12-21 16:14:31

+0

Oooops ......我完全忘了接受的答案...对不起><” – MademoiselleLenore 2011-12-21 17:08:47

+2

你不需要因为道歉你已经修复了这个问题:) – 2011-12-21 18:29:21

回答

3

我相信我在回应部分地回答了这个问题到one of your previous questions,所以让我们看看这次我的回答是否更好。

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

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

  2. 可以拿到联new_produit记录使用N:N表和ID在步骤1中

  3. 检索一旦你有了新new_lignecontrat的ID和的最后相关new_produit记录的ID,针对这些记录及其关系执行IOrganizationServiceAssociate方法。以下是处理这三个步骤的模型。

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); 
} 
+0

嗨,彼得,谢谢。你之前的回答并不差,而且非常有帮助。我被告知queryexpressions是实现我们想要的最好的方法,但我发现Linq方法更好地编码。 – MademoiselleLenore 2011-12-22 14:27:33