如何分解EntityReferenceCollection?

时间:2018-08-02 17:00:07

标签: dynamics-crm microsoft-dynamics dynamics-365

我正在编写一种将EntityReferenceCollection分解为指定块大小的方法。我有列表的代码,但不知道如何为EntityReferenceCollection请帮助!

public static List<List<T>> Split<T>(List<T> collection, int size)
        {
            var chunks = new List<List<T>>();
            var chunkCount = collection.Count() / size;

            if (collection.Count % size > 0)
                chunkCount++;

            for (var i = 0; i < chunkCount; i++)
                chunks.Add(collection.Skip(i * size).Take(size).ToList());

            return chunks;
        }

我正在尝试批量删除:

string fetchXml = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                        <entity name='new_units'>
                            <link-entity name='new_alterunitorder' from ='new_orderlineid' to = 'new_unitsid' >
                                <attribute name='new_alterunitorderid' />
                                <filter type='and'>
                                    <condition attribute='new_orderdate' operator='on-or-after' value='" + startDate.ToShortDateString() + @"' />
                                    <condition attribute='new_orderdate' operator='on-or-before' value='" + endDate.ToShortDateString() + @"' />
                                    <condition attribute='new_orderlineid' operator='eq' uiname='" + uiName + @"' uitype='new_units' value='" + unitOrderId + @"' />
                                </filter>
                            </link-entity>
                        </entity>
                    </fetch>";

                    EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));

                    var entRefCollection = new EntityReferenceCollection();

                    foreach (var id in result.Entities)
                    {
                        var reference = id.GetAttributeValue<EntityReference>("new_alterunitorderid");
                        entRefCollection.Add(reference);
                    }

                    if(entRefCollection.Count < 1000)
                    {
                        Bulk.BulkDelete(service, entRefCollection);
                    }
                    else
                    {
                        Bulk.BulkDelete(service, entRefCollection_one);  //1st half of EntityReferenceCollection
                        Bulk.BulkDelete(service, entRefCollection_two);

                    }

1 个答案:

答案 0 :(得分:1)

感谢您的更新。我还刚刚发布了关于您有关批量删除的其他问题的答案。

简而言之,如果您要自己在代码中执行此操作,则可以使用FetchXML分页cookie下载1000条记录的批处理,然后也执行1000条删除的多请求批处理。

我发现LINQ中的分页比FetchXML友好一些,因此您可能希望切换到LINQ query,并通过与List示例中使用的语法相同的类型进行分页: Skip(i * size).Take(size)

或者,正如我在另一个问题上发布的那样,您可以提交批量删除作业。