This question already has an answer here:
这个问题在这里已有答案:
- Split a collection into `n` parts with LINQ? 19 answers
- 使用LINQ将集合拆分为`n`部分? 19个答案
- Split a List into smaller lists of N size 10 answers
- 将列表拆分为N个10个答案的较小列表
I have a program that will read the contents of a file into some sort of list or array. This list/array could contain any number of items. I need to split it into smaller groups, say of 50 items each and then do some processing on each item in each group.
我有一个程序,将文件的内容读入某种列表或数组。此列表/数组可以包含任意数量的项目。我需要将它分成更小的组,每组50项,然后对每组中的每个项目进行一些处理。
List<string> stuffFromFile = new List<string>();
while ((line = fileReader.ReadLine()) != null)
{
stuffFromFile.Add(line);
}
I have been looking through some examples online about how to chunk stuff but to be honest, I don't really understand the examples and some of them seem overly complex. I just need something simple that will chunk/split/break the original list of items into groups of 50 and then let me iterate through each item in each group until the processing is complete.
我一直在网上查看一些关于如何分块的例子,但说实话,我并不真正理解这些例子,其中一些似乎过于复杂。我只需要一些简单的东西,将原始的项目列表分成/拆分/分成50个组,然后让我遍历每个组中的每个项目,直到处理完成。
The total number of items read in will most likely not be a number that I can divide evenly by 50 so most likely the last group may contain less than 50 items, but would still need to be processed just like the rest.
读入的项目总数很可能不是我可以均匀划分50的数字,因此最后一组最有可能包含少于50个项目,但仍需要像其他项目一样进行处理。
Can anyone help here? It sounds like it should be simple but I don't really know how to do it. I've seen example about using LINQ but I don't understand it either.
有人可以帮忙吗?这听起来应该很简单,但我真的不知道该怎么做。我见过关于使用LINQ的例子,但我也不理解它。
2 个解决方案
#1
8
Here's an extension method that will work with any list and any size chunks.
这是一个扩展方法,可以使用任何列表和任何大小的块。
public static List<List<T>> SplitList<T>(this List<T> me, int size = 50)
{
var list = new List<List<T>>();
for (int i = 0; i < me.Count; i += size)
list.Add(me.GetRange(i, Math.Min(size, me.Count - i)));
return list;
}
Use it like this:
像这样用它:
List<List<string>> chunksOf50 = stuffFromFile.SplitList();
#2
1
List<string> stuffFromFile = new List<string>() { "1", "2", "3", "4" }; //contents
while (stuffFromFile.Count > 0)
{
List<string> newChunk = stuffFromFile.Take(50).ToList(); //Take up to 50 elements
stuffFromFile.RemoveRange(0, newChunk.Count); // Remove the elements you took
}
#1
8
Here's an extension method that will work with any list and any size chunks.
这是一个扩展方法,可以使用任何列表和任何大小的块。
public static List<List<T>> SplitList<T>(this List<T> me, int size = 50)
{
var list = new List<List<T>>();
for (int i = 0; i < me.Count; i += size)
list.Add(me.GetRange(i, Math.Min(size, me.Count - i)));
return list;
}
Use it like this:
像这样用它:
List<List<string>> chunksOf50 = stuffFromFile.SplitList();
#2
1
List<string> stuffFromFile = new List<string>() { "1", "2", "3", "4" }; //contents
while (stuffFromFile.Count > 0)
{
List<string> newChunk = stuffFromFile.Take(50).ToList(); //Take up to 50 elements
stuffFromFile.RemoveRange(0, newChunk.Count); // Remove the elements you took
}