Reputation: 43
I'm new to .NET Core, and I'm reading this doc https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.1
From there I'm practicing - I write this logic:
public async Task<ActionResult<List<DashBoar>>> GetAllAsync()
{
var x = _Repo.GetPetsAsync();
return await x.ToList();
}
But I'm getting an error.
My repo class is
public IEnumerable<DashBoar> GetPetsAsync()
{
var x = from n in _context.DashBoar
select n;
return x.ToList();
}
Upvotes: 3
Views: 8269
Reputation: 106
You should first understand what asynchronous programming is and the co-relation of await, async and Task.
Asynchronous programming is used to improve the application performance and enhance the responsiveness. Refer the links at the bottom to get an understanding.
First let's address your problem. Make your repo class return type as a Tak
public async Task<IEnumerable<DashBoar>> GetPetsAsync()
{
var x = await (from n in _context.DashBoar
select n).ToListAsync();
return x;
}
Then call the repo method from GetAllAsync() method as below
public async Task<ActionResult<List<DashBoar>>> GetAllAsync()
{
var x = await _Repo.GetPetsAsync();
return x;
}
Please go through the below links To get a better understanding of asynchronous programming.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/ https://www.youtube.com/watch?v=C5VhaxQWcpE
https://www.dotnetperls.com/async
Good luck..!
Upvotes: 7