I have a List of Lat/Lon co-ordinates that I’m processing in a while(true) loop. During the loop I’m building a query that will be sent to a remote service for processing. The remote service can only accept 12 pairs of Lat/Lon co-ordinates, however my list may contain several thousand. What I want to do is build the query and then send it for processing every 12 loops.
List<string[]> lList = FromDB();
int i = 0;
int intLastIndex - lList.Count;
string strQuery = String.Empty
while(true)
{
strQuery = lList[i][0] + "|" + lList[i][1];
if(((i % 11) == 0) && (i != 0))
{
SendToRemoteService(strQuery);
strQuery = String.Empty;
}
if(i == intLastIndex)
{
break;
}
i++
}
However this generate an array out of bounds exception and will not process all records. Can anyone suggest a better approach?
Mark
I have found at least 5 bugs in your code, you should rewrite it to:
But if you want to upgrade to C# 3.0 you can use System.Linq, aka Linq-to-objects, which will simplify your code. Imagine you would want to send a single pair per request, then your code would be:
Now grouping into groups of 12 pairs:
code style note: many people would rather use
queryinstead ofstrQueryas a variable name.