Solved

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

Posted on 2010-11-15
2
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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