-1

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!

Sunny
  • 3,185
  • 8
  • 34
  • 66
Ryan Ayala
  • 77
  • 1
  • 8

3 Answers3

2

After your first loop, you have to call:

files = Files.Skip(10).Take(10).ToList();
Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • sure - dont forget to flag the question answered if it worked for you :) – Pedro G. Dias Mar 15 '16 at 07:37
  • After processing all 25 PDF Files. when Foreach loops again the 5 PDF left still on Files. – Ryan Ayala Mar 15 '16 at 08:21
  • Yes, take() will give you up to the number of records, but if there are only 5 left, you get those 5. You can do a simple check to count how many you have in your logic, i.e. if(files.Count() == 10) ... – Pedro G. Dias Mar 15 '16 at 08:42
1

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))
{
   //....
}
quadroid
  • 8,444
  • 6
  • 49
  • 82
0

I am using your code and made some changes to make it working for more than 20 files if you need.

  // Files has 25 PDF
        var Files = Folder.GetFileToPublicFolder(Folder.srcFolder);

        int _skip = 0;

        foreach (FileInfo file in Files)
        {
            // Get 10 PDF in Files
            List<FileInfo> files = Files.Skip(_skip).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);
            _skip = _skip + 10;
        }

Hope it will work for you.

Sunny
  • 3,185
  • 8
  • 34
  • 66