Reputation: 3
I have a while loop that continues to pull data from a query until the query returns no more results, within this the for each loop populates each returned data into an xml file. The query has a limit of 500 results per query so I wanted to return 500 results populate them and query again for the next 500 until the query return code < 1 (no results)
I am tracking the id and using this in the query to get the logic, however once the first 500 results return the loop does not get the next 500.
What I am doing wrong?
code:
bool is_finished = false;
while (is_finished == false)
{
string current_id = "0";
sb.Append("<queryxml><entity>Account</entity><query><field>AccountNumber<expression op='greaterthan'>" + current_id + "</expression></field></query></queryxml>").Append(System.Environment.NewLine);
var r = client.query(at_integrations, sb.ToString());
if (r.ReturnCode == 1)
{
foreach (var item in r.EntityResults) // max EntityResults 500 per query
{
Account acct = (Account)item;
//create xml file with the results
current_id = acct.id.ToString();
}
}
else
{
is_finished = true;
}
}
Upvotes: 1
Views: 160
Reputation: 22029
You have the definition of current_id
within your loop, each iteration it will reset it to zero.
You should debug this, you could quite easily add Debug.WriteLine(sb);
to see the contents and you'd see that it would always return the same (note that current_id
is always 0):
<queryxml><entity>Account</entity><query><field>AccountNumber<expression op='greaterthan'>0</expression></field></query></queryxml>
Here's updated code, which simply moves the definition of current_id
out of the loop, so that it can be updated and the value will stick.
bool is_finished = false;
string current_id = "0";
while (is_finished == false)
{
sb.Append("<queryxml><entity>Account</entity><query><field>AccountNumber<expression op='greaterthan'>" + current_id + "</expression></field></query></queryxml>").Append(System.Environment.NewLine);
var r = client.query(at_integrations, sb.ToString());
<snip>
Upvotes: 1