(不,不是法國姑娘再次!!!)檢索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);
作爲一個法國女孩是沒有理由不接受的答案;) – 2011-12-21 16:14:31
Oooops ......我完全忘了接受的答案...對不起><」 – MademoiselleLenore 2011-12-21 17:08:47
你不需要因爲道歉你已經修復了這個問題:) – 2011-12-21 18:29:21