Need some help with Iteration

justindoh

New member
Joined
Dec 12, 2021
Messages
4
Programming Experience
1-3
Sorry, I am not good at C#..

I am trying to write Looping for the result of data collected from each API call.

Because it is API, it has certain limitation ("PageSize") to collect data each time.
Currently, it has "PageSize" of 2000, and it increments by that value (2000).
"Offset" points where data retrieval starts.

I am trying to write For or For Each or whatever that collects all data together until "clinet.Execute(query)" ends.
By looking at the data, it appears that "result.NumRemaining = 0 or response.Results.NumRemaining = 0" should be the end point where looping should end.

Below is image of where objects for the query are declared and I personally wrote two more lines to declare two objects (where it starts with: int? offset.. and int? pagesize..) to be used for looping.
I am not sure these two lines (where it starts with: int? offset.. and int? pagesize..) should go before it has "Result result = response.Result[0]" or not.

I am stuck how to use "result.NumRemaining = 0" to used inside Looping and also how to express to collect data together.

1639321803365.png


Bottom is whole code:

LoopQuestion:
public static string Run(ILogger logger)    
        {
            OnlineClient client = Bootstrap.Client(logger);

            SelectBuilder selectBuilder = new SelectBuilder();
            ISelect[] fields = selectBuilder.
                Fields(new[] { "RECORDNO","VENDORID", "VENDORNAME", "TOTALPAID"}).
                GetFields();

            // trying to increase Offset by 2,000 each time until result = 0
            QueryFunction query = new QueryFunction()
            {                
                FromObject = "APBILL",            
                Offset = 0,         // (0, 2000, 4000, 6000 etc..)          
                PageSize = 2000,
                SelectFields = fields
            };
                     
            Task<OnlineResponse> task = client.Execute(query);        
            OnlineResponse response = task.Result;

            // ***** I wrote these two following lines..
            int? offset = query.Offset;
            int? pagesize = query.PageSize;

            Result result = response.Results[0];

            //for (offset = 0; response.Result = 0; pagesize++)  <--- ***** I tried to write Looping here....
            // Get the result
         

            // Convert into Json
            if (result.Count > 0)
                {
                dynamic resultJson =
                       JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result.Data));
                string resultJsonString = resultJson.ToString();
                return resultJsonString;
                }
            return "";
        }

How do I express Looping?

Thanks.
 
Last edited:
Use a do-while loop...

In pseudo-code:
C#:
int offset = 0;
int pageSize = 2000;
int remaining = 0;
var query = new QueryFunction() { ... }

do
{
    query.Offset = offset;
    query.PageSize = pageSize;

    client.Execute(query);

    offset += pageSize;
    remaining = results.NumRemaining;
    pageSize = min(pageSize, remaining);

    // do something with results

} while (remaining > 0);
 
@Skydiver
Thank you for your help and quick response.

I modified my code, and this is what I have on the bottom.

It appears that on line 35, I am getting error at "min".
1639328328952.png


Please let me know if this code looks right.
Thank you so much!

Modified Code:
public static string Run(ILogger logger)    
        {
            OnlineClient client = Bootstrap.Client(logger);

            SelectBuilder selectBuilder = new SelectBuilder();
            ISelect[] fields = selectBuilder.
                Fields(new[] { "RECORDNO","VENDORID", "VENDORNAME", "TOTALPAID"}).
                GetFields();

            int offset = 0;
            int pagesize = 2000;
            int remaining = 0;

            var query = new QueryFunction()
            {
                FromObject = "APBILL",
                Offset = 0,         // (0, 2000, 4000, 6000 etc..)          
                PageSize = 2000,
                SelectFields = fields
            };

            Task<OnlineResponse> task = client.Execute(query);
            OnlineResponse response = task.Result;
            Result result = response.Results[0];

            do
            {
                query.Offset = offset;
                query.PageSize = pagesize;

                client.Execute(query);

                offset += pagesize;
                remaining = result.NumRemaining;
                pagesize = min(pagesize, remaining);

                // do something with results
                if (result.Count > 0)
                {
                    dynamic resultJson =
                           JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result.Data));
                    string resultJsonString = resultJson.ToString();
                    return resultJsonString;
                }

            } while (remaining > 0);

            return "";
        }
 
Last edited:
As I said, it's pseudo code. It was up to you to find the appropriate C# equivalent. Recall that C# doesn't have a min() keyword, but the framework has a method that will do what you want.

Also take a close look at your code, what is the point of lines 22-24? Why are you not picking up the response on line 31?
 
Last edited:
@Skydiver
I am sorry. I need some guideline.

I am not sure exactly what these lines do:
27: Task<OnlineResponse> task = client.Execute(query);
28: OnlineResponse response = task.Result;
29: Result result = response.Results[0];

Does this code correct?

Modified Code:
public static string Run(ILogger logger)    
        {
            OnlineClient client = Bootstrap.Client(logger);

            SelectBuilder selectBuilder = new SelectBuilder();
            ISelect[] fields = selectBuilder.
                Fields(new[] { "RECORDNO","VENDORID", "VENDORNAME", "TOTALPAID"}).
                GetFields();

            int offset = 0;
            int pagesize = 2000;
            int remaining = 0;

            var query = new QueryFunction()
            {
                FromObject = "APBILL",
                Offset = 0,         // (0, 2000, 4000, 6000 etc..)          
                PageSize = 2000,
                SelectFields = fields
            };

            do
            {
                query.Offset = offset;
                query.PageSize = pagesize;

                Task<OnlineResponse> task = client.Execute(query);
                OnlineResponse response = task.Result;
                Result result = response.Results[0];

                offset += pagesize;
                remaining = result.NumRemaining;
                pagesize = Math.Min(pagesize, remaining);

                // do something with results
                if (result.Count > 0)
                {
                    dynamic resultJson =
                           JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result.Data));
                    string resultJsonString = resultJson.ToString();
                    return resultJsonString;
                }
            } while (remaining > 0);

            return "";
        }

Thanks.
 
Last edited:
You wrote the code in your original post and don't know what it does? Should you really be dealing with people's financial information if you don't know what you are doing?
 
@Skydiver Thank you for your feedback.
Yea. I am using the Sage API, and I need to figure out what options they offer (other functions in the library).
From C# programming stand point, I would dig in more with debugging to get a better idea on each steps. Thank you so much!
 
Back
Top Bottom