首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何批量删除两个日期之间的记录?

如何批量删除两个日期之间的记录?
EN

Stack Overflow用户
提问于 2018-08-02 21:54:45
回答 2查看 902关注 0票数 2

我想删除两个日期之间的记录。我使用一个Fetchxml表达式来获取这两个日期之间的每个记录的GUID,然后使用service.Delete。下面是我的代码:

代码语言:javascript
复制
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));

                    //Delete the collection 
                    foreach (var id in result.Entities)
                    {
                        var entRef = id.GetAttributeValue<EntityReference>("new_alterunitorderid");
                        var guid = (Guid)entRef.Id;
                        service.Delete("new_alterunits", guid);
                    }

FetchXML有5000条记录的限制,我怎么能绕过它呢?!我还了解到,使用service.Delete遍历不是推荐的批量删除方式。请帮我找到实现我想做的事情的最好方法!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-03 03:39:43

现在我们已经明确了需求,下面是我将如何解决这个问题的要点。它使用了一个简化的FetchXML查询,但您将会领会其中的含义。

请注意,在Fetch中添加了top='2000'

代码语言:javascript
复制
var fetchXml = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' top='2000'>
                     <entity name='new_sampledata' />                                  
                   </fetch>";

var result = svc.RetrieveMultiple(new FetchExpression(fetchXml));

var entityRefs = result.Entities.Select(e=> e.ToEntityReference());

//instead of e.ToEntityReference(), you'd use e.GetAttributeValue<EntityReference>("new_alterunitorderid")
//like this:
//var entityRefs = result.Entities.Select(e=> e.GetAttributeValue<EntityReference>("new_alterunitorderid"));

var batchSize = 1000;
var batchNum = 0;
var numDeleted = 0;

while (numDeleted < entityRefs.Count())
{
    var multiReq = new ExecuteMultipleRequest()
    {
        Settings = new ExecuteMultipleSettings()
        {
            ContinueOnError = false,
            ReturnResponses = false
        },
        Requests = new OrganizationRequestCollection()
    };

    var currentList = entityRefs.Skip(batchSize * batchNum).Take(batchSize).ToList();

    currentList.ForEach(r => multiReq.Requests.Add(new DeleteRequest { Target = r }));

    svc.Execute(multiReq);

    numDeleted += currentList.Count;
    batchNum++;
}
票数 2
EN

Stack Overflow用户

发布于 2018-08-03 02:06:51

您可能还希望了解如何通过BulkDeleteRequest类使用本机批量删除服务。

下面是一个基本的例子,this article有一个更详细的例子。如果要将FetchXML转换为QueryExpression,可以使用FetchXmlToQueryExpressionRequest类。

代码语言:javascript
复制
// Set the request properties.
bulkDeleteRequest.JobName = "Backup Bulk Delete";

// Querying activities
bulkDeleteRequest.QuerySet = new QueryExpression[]
{
    opportunitiesQuery,
    BuildActivityQuery(Task.EntityLogicalName),
    BuildActivityQuery(Fax.EntityLogicalName),
    BuildActivityQuery(PhoneCall.EntityLogicalName),
    BuildActivityQuery(Email.EntityLogicalName),
    BuildActivityQuery(Letter.EntityLogicalName),
    BuildActivityQuery(Appointment.EntityLogicalName),
    BuildActivityQuery(ServiceAppointment.EntityLogicalName),
    BuildActivityQuery(CampaignResponse.EntityLogicalName),
    BuildActivityQuery(RecurringAppointmentMaster.EntityLogicalName)
};

// Set the start time for the bulk delete.
bulkDeleteRequest.StartDateTime = DateTime.Now;

// Set the required recurrence pattern.
bulkDeleteRequest.RecurrencePattern = String.Empty;

// Set email activity properties.
bulkDeleteRequest.SendEmailNotification = false;
bulkDeleteRequest.ToRecipients = new Guid[] { currentUserId };
bulkDeleteRequest.CCRecipients = new Guid[] { };

// Submit the bulk delete job.
// NOTE: Because this is an asynchronous operation, the response will be immediate.
_bulkDeleteResponse =
    (BulkDeleteResponse)_serviceProxy.Execute(bulkDeleteRequest);
Console.WriteLine("The bulk delete operation has been requested.");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51655422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档