Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a String Array that has 5000 Indexes. now I want to loop through them but only 1300 indexes at time until the end of string array how can I do this?

this is what I have so far

String [] MyArray= new string [5000]; // this is just and example in my program the length of my string is determined at runtime  so I have no way of knowing how long the string array will be.

Now I want to loop through it something like this

first time around get Index [0] through [1299] seconds time around get Index [1300] through [2599] and so on until the end of array Index.

How can I do this.

here is my code

  DataView fin = new DataView(ds.Tables["domestic"]);

        DataView TempXml = new DataView(Xml);
        DataTable OneOzXml = Xml.Clone();
        int o = 0;
        string[] smn = OneOunce.ToString().Split(' ');
        int totalRecords= smn.Length-1;
       for (int t = 0; t < smn.Length-1; t++)
       {

           fin.RowFilter = "Statementnumber=" + smn[t];
           TempXml.RowFilter = "Statementnumber=" + smn[t];

           for (int i = 0; i <= fin.Count - 1; i++)
           {
               OneOz.ImportRow(fin[i].Row);

           }
           for (int i = 0; i <= TempXml.Count-1 ; i++)
           {
               OneOzXml.ImportRow(TempXml[i].Row);
           }
           o++;
           while (totalRecords!=0)

               if (o == 1300)
           {
               int filenumber=1;
               string Filename = "MArtin_1oz_" + filenumber;
               XmlFilename = @"d:\XML\" + Filename + ".xml";
               System.IO.FileStream streamWrite = new System.IO.FileStream(XmlFilename, System.IO.FileMode.Create);
               OneOzXml.WriteXml(streamWrite);
               streamWrite.Close();
               XElement xEle = XElement.Load(@"d:\XML\" + Filename + ".xml");
               var snumber = xEle.Elements("MetaDataDetail").Elements("STATEMENTNUMBER").ToList();
               foreach (XElement Statementnumber in snumber)
                   Statementnumber.Remove();
               xEle.Save(@"d:\XML\"+Filename+".xml");
               reportDoc.SetDataSource(OneOz);
               CrDiskFileDestinationOptions.DiskFileName = @"D:\pdf\"+Filename+".pdf";
               reportDoc.Export();
               OneOz.Clear();
               OneOzXml.Clear();

               PdfDocument AddCustomerPackage = new PdfDocument();
               AddCustomerPackage = PdfReader.Open(@"D:\pdf\" + Filename + ".pdf", PdfDocumentOpenMode.Modify);

               ffp WritePackageNumber = new ffp();


               WritePackageNumber.FindFirstPage(AddCustomerPackage, "Page 1");
             filenumber++;
             //int pageCount = 1;
             //int counter = document.PageCount;
             //PdfSharp.Pdf.PdfPage Pages;

             //for (int idx = 0; idx < counter; idx++)
             //{
             //    Pages = document.Pages[idx];
             //    gfx = XGraphics.FromPdfPage(Pages);
             //    gfx.DrawString(pageCount.ToString("00000"), font, XBrushes.Black,

             //                         new XRect(246, 140, Pages.Width, Pages.Height),
             //                         XStringFormats.TopLeft);
             //    pageCount++;
             //}

             //document.Save(document.FullPath);
             totalRecords = totalRecords - o;
             o = 0;
share|improve this question
10  
Not going to do your homework but I will tell you a for-loop can help you accomplish this. – Tdorno May 31 at 13:18
3  
Why specifically in batches? There is clearly a problem to solve behind this need, if we knew what that was we could offer better advice. – Adam Houldsworth May 31 at 13:19
2  
What have you tried so far? – crush May 31 at 13:19
4  
Your question doesn't show much evidence of effort on your part. Have you tried anything yourself? What you have so far doesn't even include a loop - I would imagine you can get that far, at least. Try adding a loop and then work out what values you need to pass into the loop to get it to work. – Dan Puzey May 31 at 13:19
@ TDorno .. It's not homework – user2315840 May 31 at 13:21
show 1 more comment

closed as not a real question by dandan78, duDE, Jens, JW 웃, Andrew Barber Jun 2 at 17:08

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

5 Answers

you can use a for loop and increment:

for (int i = 0; i < 5000; i+=1300)
{
    var mysubset = myArray.Skip(i)
                          .Take(1300).ToArray();
}
share|improve this answer
1  
If the idea is just to loop through each string, it's probably better to avoid calling ToArray – vc 74 May 31 at 13:30
@vc74 agreed, I was just putting that in there because OP seemed to want an array. I would keep this as an enumerable, myself. – Jeremy Holovacs May 31 at 13:32

There is a simple solution, use Batch from MoreLINQ

MyArray.Batch(1300).Select(items => /* handle 1300 items*/)
share|improve this answer
+1: cleaner than for loop. – newStackExchangeInstance May 31 at 13:22
4  
Why reference a whole new library to save a line of code? C# handles this use case pretty well without that. – Jeremy Holovacs May 31 at 13:24
yes but what will happen if the last run does not have 1300 records? – user2315840 May 31 at 13:25
@JeremyHolovacs yes, sure, and iterate over sequence of data over and over again in each for iteration. In case of array this is ok, but what about remote sequences, mapped data, etc? – Ilya Ivanov May 31 at 13:27
@IlyaIvanov, I'm sure there are many use cases where this library is useful, justified and appropriate; I just don't think this is one of them. – Jeremy Holovacs May 31 at 13:28
show 3 more comments

You can partition your array as:

var arrays = MyArray.Select((i, inx) => new { i, inx })
                    .GroupBy(x => x.inx / 1300)
                    .Select(g => g.Select(y=>y.i).ToList())
                    .ToList();

arrays will contain 4 Lists with sizes 1300,1300,1300,1100

share|improve this answer
I like this approach the most ! +1 – Dimitar Dimitrov May 31 at 13:31
At the end of this though, you still have 2 collections to loop through. Not saying that's a bad thing, but it seems a lot more code than is necessary. – Jeremy Holovacs May 31 at 13:35
const int pageSize = 1300;
int pagesCount = (MyArray.Length / pageSize) + 1;
IEnumerable<IEnumerable<string>> pages = Enumerable.Range(0, pagesCount).
                      Select(page => MyArray.Skip(page * pageSize).Take(pageSize));

foreach (IEnumerable<string> page in pages)
{
  // Do something with the 1300 strings
} 

Take will return pageSize strings at maximum, but probably less for the last page.

share|improve this answer

I would probably do something like:

int arraySize = 20000;
int set = 0;
for (int i = set * 1300; i < (set + 1) * 1300 && i < arraySize; i++)
{ 
   //process string 
}

Where arraySize is the size of the array. This allows you to batch handle the array in 1200 increments. If you want to handle the first 1300, set will be 0. If you wanted to edit the 5th set of 1300, then set would be 4. This also prevents you from going out of bounds.

You could put the actual loop in a method so that it is reusable. I just have it like this for demonstration.

share|improve this answer
My Problem is a bit Different. Look at the code I have pasted in my question. Once the 1300 records are processed than only the dataset are given to export to xml and pdfsharp. this will work fine until I hit the last set where my O variable will not have 1300 records so it will just skip those records. how can I solve this \ – user2315840 May 31 at 14:27
@user2315840 Can you pad the string array to always have a multiple of 1300 in it? Then the last dataset would have some blank values, but it would be a full 1300. – Mike May 31 at 18:18
I am close to finding the solution, it works but last run has one extra index in the array that is breaking the program. – user2315840 May 31 at 19:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.