Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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