Solved

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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