here is the code :
// Files has 25 PDF
var Files = Folder.GetFileToPublicFolder(Folder.srcFolder);
foreach (FileInfo file in Files)
{
// Get 10 PDF in Files
List<FileInfo> files = Files.Take(10).ToList();
// Process the 10 PDF
foreach (var item in files)
{
File.Move(Path.Combine(Folder.srcFolder, item.Name), Path.Combine(Folder.tmpFolder, item.Name));
}
files = null;
ProcessParallelThread(e);
}
I have Public Folder that has 25 PDF Files.
Using this
List<FileInfo> files = Files.Take(10).ToList();
it will get the 1 - 10 PDF and processed it. After processing the 1 - 10 PDF when foreach loops again it take the same 1 - 10 PDF not the 11 - 20 PDF.
How can i get the Other PDF in List<>?
Thank you in Advance!
Take does as the name suggest just take 10 elements (the first 10 to be more specific) there is no way that the 2nd take call does take different 10 elements if you don't tell it.
If you want to process chunks of 10 Elements i would suggest creating a method that does the splitting (Separation of Concerns)
This is what i use:
public static class EnumerableExtensions
{
[Pure]
public static IEnumerable<T[]> Split<T>(this IEnumerable<T> source, int chunkSize)
{
T[] sourceArray = source as T[] ?? source.ToArray();
for (int i = 0; i < sourceArray.Length; i += chunkSize)
{
T[] chunk = new T[chunkSize + i > sourceArray.Length ? sourceArray.Length - i : chunkSize];
Array.Copy(sourceArray, i, chunk, 0, chunk.Length);
yield return chunk;
}
}
}
now you got a split method which can be used like this
var files = Folder.GetFileToPublicFolder(Folder.srcFolder);
foreach(var chunk in files.Split(10))
{
//....
}