Reputation: 18694
I am attempting to learn how to use await and async.
I have a service layer, which has a reference data manager. My interface is:
public interface IReferenceDataService
{
Task<List<ReferenceItemDto>> GetAsync(ReferenceTypes type);
}
When I try get my data in the UI, I am doing this:
model.DeptPaymentTypes = _refDataService.GetAsync(Enums.ReferenceTypes.DeptPaymentTypes)
.Select(x => new SelectListItem {
Text = x.Description,
Value = x.Id.ToString() })
.ToList();
But am getting an error that, "ToList is not a definition for Task<..."
My data layer calls gets the data using Dapper QueryAsync...
public async Task<List<ReferenceItemDto>> GetAsync(Enums.ReferenceTypes type)
{
var table = string.Empty;
if(type == Enums.ReferenceTypes.DaysOfMonth)
{
var days = new List<ReferenceItemDto>();
for (int i = 1; i <= 31; i++)
{
days.Add(new ReferenceItemDto
{
Description = i.ToString(),
Id = i
});
}
return days;
}
switch (type)
{
case Enums.ReferenceTypes.SnowballTypes:
table = "SnowballType";
break;
case Enums.ReferenceTypes.DeptPaymentTypes:
table = "DebtPaymentType";
break;
default:
throw new System.Exception("Unknown data type in referenc manager.");
}
using (IDbConnection db = new SqlConnection("Data Source=......"))
{
var data = await db.QueryAsync<ReferenceItemDto>("GetReferenceDataList", new { DataType = table }, commandType: CommandType.StoredProcedure);
return data.ToList();
}
}
What am I doing wrong? The ToList is causing me an issue.
Upvotes: 3
Views: 7733
Reputation: 3380
You should get a result from your async method.
It's better to avoid blocking calls like Result
, so use await
instead.
model.DeptPaymentTypes = (await _refDataService.GetAsync(Enums.ReferenceTypes.DeptPaymentTypes))
.Select(x => new SelectListItem {
Text = x.Description,
Value = x.Id.ToString() })
.ToList();
Upvotes: 8
Reputation: 1087
Try to use
model.DeptPaymentTypes = _refDataService.GetAsync(Enums.ReferenceTypes.DeptPaymentTypes).Result
.Select(x => new SelectListItem {
Text = x.Description,
Value = x.Id.ToString() })
.ToList();
Upvotes: 3