2011-12-21 79 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