我有一个从SharePoint列表中获取x个数字和项目的函数。分批处理项目。每一批之后,我会对这些物品进行处理,销毁所有物品,然后进行下一批计算。我目前正在考虑使用事件。因此,每个批次都有一个事件。这是正确的策略还是有更好的方法呢?我在考虑匿名功能或类似功能吗?
public static List<Item> GetAllItems(this List list, int rowLimit, List<string> fields, bool includeRoleAssignments, ILogger logger)
{
var result = new List<Item>();
var ctx = list.Context;
ListItemCollectionPosition position = null;
var camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View Scope='RecursiveAll'>
<Query>
<OrderBy Override='TRUE'><FieldRef Name='ID'/></OrderBy>
</Query>
<ViewFields></ViewFields>" +
"<RowLimit Paged='TRUE'>" + rowLimit + "</RowLimit>" +
"</View>";
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
do
{
try
{
using (var clonedCtx = ctx.Clone(ctx.Url))
{
List listWithClonedContext = clonedCtx.Web.Lists.GetByTitle(list.Title);
clonedCtx.Load(listWithClonedContext);
clonedCtx.ExecuteQuery();
ListItemCollection listItems = null;
camlQuery.ListItemCollectionPosition = position;
listItems = listWithClonedContext.GetItems(camlQuery);
foreach (string field in fields)
{
clonedCtx.Load(listItems, includes => includes.Include(i => i[field]));
}
if (!includeRoleAssignments) {
clonedCtx.Load(listItems, item => item.ListItemCollectionPosition);
}
else {
clonedCtx.Load(listItems, item =>
item.ListItemCollectionPosition,
item => item.Include(
i => i.RoleAssignments.Include(
ra => ra.Member,
ra => ra.Member.LoginName,
ra => ra.RoleDefinitionBindings.Include(rd => rd.Description, rd => rd.Name))));
}
clonedCtx.Load(listItems, item => item.ListItemCollectionPosition);
clonedCtx.ExecuteQueryWithIncrementalRetry(3, 1, logger);
// here i want to do something with items before next loop/batch
position = listItems.ListItemCollectionPosition;
if (position != null)
{
logger.WriteTrace(string.Format("Iteration on getting items performed: {0}", position.PagingInfo), SeverityLevel.Verbose);
}
else
{
logger.WriteTrace("Getting all items finished.", SeverityLevel.Verbose);
}
logger.Flush();
}
}
catch (Exception ex)
{
logger.WriteException(ex);
}
}
while (position != null);
return result;
}
答案 0 :(得分:1)
也许可以选择使用事件,但也可以使用一种更简便的方法将其“流式传输”出来,而不是一次返回所有列表。因此,请使用yield
并更改为IEnumerable<Item>
:
public static IEnumerable<Item> EnumerateItems(this List list, int rowLimit, List<string> fields, bool includeRoleAssignments, ILogger logger)
{
// ...
do
{
try
{
using (var clonedCtx = ctx.Clone(ctx.Url))
{
//...
camlQuery.ListItemCollectionPosition = position;
listItems = listWithClonedContext.GetItems(camlQuery);
// ...
foreach(Item x in listItems)
{
yield return x;
}
position = listItems.ListItemCollectionPosition;
// ...
}
while (position != null);
}
通过这种方式,您可以在仍在获取它们的同时开始处理它们,或者可以使用Where
,Skip
或Take
对其进行过滤,而无需先将它们全部加载到内存中。