如何分解 EntityReferenceCollection?

How To Break Up EntityReferenceCollection?

我正在编写一种将 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);

                    }

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

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

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

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