-1

I have this variable called tasks:

var tasks = new List<string>();

Right now tasks has a count of 81, but this could change down the road.

What I am trying to do is 3 loops of tasks getting 28 at a time like so:

@for (var i = 0; i < 28; i++)
{
}

@for (var i = 28; i < 56; i++)
{
}

@for (var i = 56; i < 81; i++)
{
}

I do not like that I hard coded the numbers, so my question is using tasks.Count What would be the best way loop through an array getting 28 at a time?

user979331
  • 11,039
  • 73
  • 223
  • 418
  • 1
    hum, don't know why you want to loop 28 at a time but why you don't split you'r array if count > 28 ? – Paul Jan 25 '19 at 16:11
  • Can I see an example of this? – user979331 Jan 25 '19 at 16:11
  • Create variables called *page* and *pageSize* with appropriate values (page should be 0 to start). Then `tasks.Skip(page * pageSize).Take(pageSize)` Now you increment page by 1 and continue doing this until you get back zero records from your skip/take. –  Jan 25 '19 at 16:13
  • I want to get 28 at a time so I can do page breaks as page-break-after does not work properly on chrome or on table rows for that matter – user979331 Jan 25 '19 at 16:13
  • @Will how would this look in loops, can you provide an answer? – user979331 Jan 25 '19 at 16:14
  • 1
    ```public void Split(T[] array, int index, out T[] first, out T[] second) { first = array.Take(index).ToArray(); second = array.Skip(index).ToArray(); }``` And call ```Split(tasks, 27, out firstsplitarray, out secondsplitarray);``` – Paul Jan 25 '19 at 16:14
  • It's not clear what you're really asking here. Your 3 `for` loops above are exactly the same as having one loop that runs through them all. – DavidG Jan 25 '19 at 16:20
  • 1
    Whether you split an array or hard code a number into your loop, you at some point have to hard code a "28" somewhere in your code, right? Unless you get the number from the user in the UI or a database or somewhere. So it's really a question of the better place to hard code the value? I'd say in the web.config – KidBilly Jan 25 '19 at 16:26
  • I want to clarify my point a little, I'm asking *what is the point of doing this?*. Looping through all items in batches of 28 is going to give exactly the same output as looping through all items one by one, other than the code being more complicated and unreadable. What are you really trying to do? – DavidG Jan 25 '19 at 16:34
  • The purpose of doing this is to split up my data into tables (28 rows) at the time. so I can apply page breaks. – user979331 Jan 25 '19 at 16:37
  • https://stackoverflow.com/questions/13731796/create-batches-in-linq – Chris Jan 25 '19 at 16:50

2 Answers2

1

Since you have clarified in the comments that what you really want to do is do something different after each block of 28 (or whatever) elements. For that you can make use of the % operator. For example:

//Keep this constant somewhere else or maybe in a config value so it is easily changed
private const int TasksPerPage = 28;

And now we can loop over your data while outputting a page break after 28 elements:

for(var i = 0; i < tasks.Count(); i++)
{   
    //You can remove the i>0 check if you want to output a break at the start
    if (i % TasksPerPage == 0 && i > 0)
    {
        Console.WriteLine("Page Break");
    }

    Console.WriteLine(tasks[i]);
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

You can use Linq to take & skip a specific amount.

var tasks = new List<string>();

var groupSize = tasks.Count() / 3;

var groupOne = tasks.Take(groupSize);
var groupTwo = tasks.Skip(groupSize).Take(groupSize);
var groupThree = tasks.Skip(groupSize * 2).Take(groupSize);

foreach(var item in groupOne)
{
    // Do Something
}


foreach(var item in groupTwo)
{
    // Do Something
}


foreach(var item in groupThree)
{
    // Do Something
}
Matt Hensley
  • 908
  • 8
  • 18