Solved

How to divide a big C# array into smaller arrays?

Posted on 2010-11-15
2
608 Views
Last Modified: 2012-05-10
Hi,

I have to send out emails using a script. Exchange server has a limit of 25 emails (cannot change, its company policy).

//My code is:
SendEmails (arrEmails());

I will appreciate if someone can please post C# code to divide the arrEmails() array into multiple arrays (if the arrEmails has more than 25 records) and use same method multiple times to perform this.

Thanks in advance.
0
Comment
Question by:m_a_n
2 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34135956
>>I will appreciate if someone can please post C# code

No doubt you would.  I'll give advice to help you help yourself.

Use a simple (while for example) loop.  
Make a new array of items start..max where max is either the limit of the server or the number of items left in the array, whichever is the least.  start is initially 0 and being incremented each time through the loop.
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 34136100
This should do the trick

DaTribe
static void Main(string[] args)
{
	SendEmails(GetEmails(97));
	Console.ReadKey();
}

private static int[] GetEmails(int max)
{
	Random r = new Random((int)DateTime.Now.Ticks);
	var a = new int[97];
	for(int i = 0; i < max; i++)
		a[i] = r.Next(100000);

	return a;
}

private static void SendEmails(int[] arrEmails)
{
	const int pageSize = 25;
	bool finished = false;

	for (int index = 0; !finished; index++)
	{
		finished = index*pageSize >= arrEmails.Length;

		if(!finished)
		{
			// Get batch
			var batch = arrEmails
				.Skip(index*pageSize)
				.Take(pageSize)
				.ToArray();

			// Process batch
		}
	}
}

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now