I am designing an API endpoint and planning to send the response with 2 values ( one with total no of records and second with list of records ) The controller action is designed like below
[HttpGet]
[Route("users_bycat")]
public async Task<ActionResult> GetUsersByCat([FromQuery] int categoryId )
{
var query = db_context.Users.Select(a => a).Where(p => p.CategoryId == categoryId);
var result = await query.ExecuteAsync();
return Ok(
new
{
totalRecords = result.Count(),
users = result.ToList()
}
);
}
When i am calling this api endpoint, i am getting response like below
{
"totalRecords": 37652,
"users": []
}
The users array is empty and i try like below , and managed to get the list as response , but i would like to add count as well to the response
return Ok( result.ToList());
How can i ensure i will get both totalRecords and users List as a part of response
query.ExecuteAsync()asquery.ToList()will fetch the result, in fact, you can remove the.Select(a => a)also.