Jeya Suriya Muthumari
Jeya Suriya Muthumari

Reputation: 2021

C#: Write Each n items from a list to a text file

I have a list of N items in a list.

var jobIdArray = new List<string>();
jobIdArray.Add(doc.FieldValues(x => x.JobId).FirstOrDefault()); // the count varies as per the result, 16000, 26000, 33598 etc

Now I need to write each 15000 into a separate single txt file. Like

for 16000 -> 15000 + 1000 for 26000 -> 15000 + 11000 for 33598 -> 15000 + 3598

I have a program like,

jobIdArray.Add(doc.FieldValues(x => x.JobId).FirstOrDefault());
                        totalCount++;
                        // for each totalCount === 15000 {write content to excel file, totalCount == 0}
                        if (totalCount == 15000) // Here i need some generic condition which suits for all number of results
                        {
                            var fileLoc = @"F:\WindowsApplication" + new Guid()+".txt";
                            //File.Create(fileLoc);
                            using (var fs = File.Create(fileLoc))
                            {
                                foreach (var item in jobIdArray)
                                {
                                    StreamWriter sw = new StreamWriter(fs);
                                    sw.Write(item);                                     
                                }
                                jobIdArray.Clear();
                                totalCount = 0;
                                fs.Close();
                            }
                        }

Any help for coding that generic condition to create text file for every 15000 would be more helpful.

Upvotes: 0

Views: 77

Answers (2)

Fabian S.
Fabian S.

Reputation: 76

You could try to iterate through your array in blocks of 15000 and then write all elements that are not emptry to the file. I didn't test it so if it's not working then just let me know and i try to figure something out.

var jobIdArray = new List<string>();
jobIdArray.Add(doc.FieldValues(x => x.JobId).FirstOrDefault());

for (int i = 0; i < jobIdArray; i += 15000)
{
    var fileLoc = @"F:\WindowsApplication" + new Guid() + ".txt";
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileLoc))
    {
        for (int x = i; x < 15000; i++)
        {
            if (!String.IsNullOrEmpty(jobIdArray[x]))
            {
                sw.WriteLine(jobIdArray[x]);
            }
        }
    }
}

Upvotes: 1

Ali Ezzat Odeh
Ali Ezzat Odeh

Reputation: 2163

Try using Linq:

void WriteGroupedItems(List<string> items,int numOfElementsPerGroup)
{
   var index = 0;
   var groupedItems = items.GroupBy(x => index++ / numOfElementsPerGroup);
   foreach(var groupedItem in groupedItems )
   {
      WriteItemsToFile(groupedItem);
   }
}

now your WriteItemsToFile may have similar logic you used, something like this:

void WriteItemsToFile(IEnumerable<string> list)
{
   var fileLoc = @"F:\WindowsApplication" + new Guid()+".txt";

   using (var fs = File.Create(fileLoc))
   {
       foreach (var item in list)
       {
          StreamWriter sw = new StreamWriter(fs);
          sw.Write(item);                                     
       }
       jobIdArray.Clear();
       totalCount = 0;
       fs.Close();
   }
}

Upvotes: 2

Related Questions