【发布时间】:2019-03-17 18:46:51
【问题描述】:
我需要批量处理子列表来批量处理对象。 Parallel.ForEach 不可行,因为批处理发布到 HTTP 请求。
public IEnumerable<T> BatchProcess<T>(IEnumerable<T> list, int batchSize)
{
int page = 0;
IEnumerable<T> batch;
while ((batch = list.Skip(page).Take(batchSize)).Count() != 0)
{
page++;
yield return batch; //Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'T'
}
}
错误 CS0029 无法将类型“System.Collections.Generic.List”隐式转换为“T”。
期望“yield return”返回一个对象列表,而不是返回单个对象。
【问题讨论】:
-
查看我个人扩展库中的“InBatches”(随意掠夺)。 github.com/thesoftwarejedi/Torian.Common/blob/master/Source/…
-
是的,它是重复的。
-
是的,只是提供一个预先编写的库来帮助您。
标签: c# yield-return