I'm trying to write a method that gathers all the results of paginated API calls for any type into a list. My method looks like this so far...
private async Task<List<T>> AggregateResults<T>(string url)
{
var results = new List<T>();
var morePages = true;
while (morePages)
{
var response = await _httpClient.GetAsync(url);
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<PaginatedResult>(responseContent);
results.AddRange(result.results.Cast<T>().ToList());
if (result.paging != null)
{
url = result.paging.next.link;
}
else
{
morePages = false;
}
}
return results;
}
the Paginated Result class looks like this
{
public class PaginatedResult
{
public object[] results { get; set; }
public Paging? paging { get; set; }
}
public class Paging
{
public Next next { get; set; }
}
public class Next
{
public string after { get; set; }
public string link { get; set; }
}
}
And I'm passing in things like this as the generic for the method
public class Person
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
}
I'm getting this error right now 'Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type .
Is there any way I can get a List from an object[] here?
I've tried casting the array about any way I can find and keep running into errors. I've thought about inheriting from PaginatedResult in my classes that I'm trying to get in lists, but can't wrap my head around how that would work or if it would.
Try making
PaginatedResulta generic classPaginatedResult<T>, and change results type fromobject[]toT[]. You can then justAddRange(result.results), no need to useCast<T>or callToList