Solved

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

Posted on 2010-11-15
2
610 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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