我如何使用这种拆分列表的方法<>
private List<List<T>> SplitPartition<T>(this IEnumerable<T> collection, int size)
{
var chunks = new List<List<T>>();
var count = 0;
var temp = new List<T>();
foreach (var element in collection)
{
if (count++ == size)
{
chunks.Add(temp);
temp = new List<T>();
count = 1;
}
temp.Add(element);
}
chunks.Add(temp);
return chunks;
}
我想将此列表分区实现为现有代码:
public void ExportToCsv()
{
List<GenerateModel> members = getDataTop5000(); // I got data from my List of Data with return List<>
int offset = 0;
const int numberPerBatch = 500000; // count parameter.
double LoopMax = Math.Ceiling(members.Count / (double)numberPerBatch);
var PartitionMembers = members.SplitPartition(numberPerBatch); //error here
while (offset < LoopMax)
{
//do the index of partion here PartitionMembers
offset++;
}
}
任何建议或示例如何使用那些方法?这真的是我需要分区到我的列表。当我尝试使用该方法时,出现如下错误:
列表不包含'SplitPartition'的定义,找不到可以接受的扩展方法'SplitPartition'接受类型为'List'的第一个参数(您是否缺少using指令或程序集引用?]
扩展方法需要属于static
类,并且
StringBuilder sb = new StringBuilder(10000);
int x= 0;
foreach(var m in members){
if(++x%50 == 0){
File.WriteAllText(sb.ToString(), $@"c:\temp\{x%50}.csv");
sb.Length = 0;
}
sb.AppendLine(m.ToCsvRepresentationEtc());
}