我有一段看起来像这样的代码:
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\DeletedItems\" + "DeletedItems" + ".xml";
XDocument xmlDoc = XDocument.Load(filePath);
var list = xmlDoc.Root.Elements("ItemID")
.Select(element => element.Value)
.ToList();
var idsList = FormItemIdList(list);
ctx.zsp_deleteEndedItems(idsList);
ctx.zsp_deleteEndedItemsTransactions(idsList);
这部分代码从XML文件加载所有项目:
var list = xmlDoc.Root.Elements("ItemID")
.Select(element => element.Value)
.ToList();
XML文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ItemsToDelete>
<ItemID>113347292264</ItemID>
<ItemID>113334066205</ItemID>
<ItemID>113331816848</ItemID>
<ItemID>113191634415</ItemID>
<ItemID>183480362055</ItemID>
<ItemID>113303425739</ItemID>
<ItemID>112533425202</ItemID>
<ItemID>112007496785</ItemID>
<ItemID>111956371906</ItemID>
<ItemID>112016647700</ItemID>
</ItemsToDelete>
将这些项目从C#加载到列表后,我形成一个字符串,如下所示:
113347292264,113334066205... etc etc
然后将字符串传递给存储过程,如下所示:
create procedure [dbo].[zsp_deleteEndedItems]
(
@ItemIDList nvarchar(max)
)
as
delete from
SearchedUserItems
WHERE EXISTS (SELECT 1 FROM dbo.SplitStringProduction(@ItemIDList,',') S1 WHERE ItemID=S1.val)
然后调用SplitStringProduction函数以分解所传递的字符串,并根据所传递的项目ID删除DB中的记录:
create FUNCTION [dbo].[SplitStringProduction]
(
@string nvarchar(max),
@delimiter nvarchar(5)
) RETURNS @t TABLE
(
val nvarchar(500)
)
AS
BEGIN
declare @xml xml
set @xml = N'<root><r>' + replace(@string,@delimiter,'</r><r>') + '</r></root>'
insert into @t(val)
select
r.value('.','varchar(500)') as item
from @xml.nodes('//root/r') as records(r)
RETURN
END
现在,所有这些都可以正常工作,但是我知道并且要避免一些问题:
现在要避免这种情况,我正在考虑执行以下操作:
因此步骤将是:
有人可以帮我解决这个问题,我不确定该怎么做?
答案 0 :(得分:1)
听起来您只是想将列表分成较小的块:
var list = xmlDoc.Root.Elements("ItemID")
.Select(element => element.Value)
.ToList();
while(list.Any())
{
var subList = list.Take(5000);
var idsList = FormItemIdList(subList);
ctx.zsp_deleteEndedItems(idsList);
ctx.zsp_deleteEndedItemsTransactions(idsList);
list.RemoveRange(subList);
}