I'm trying to implement .NET 4.5 async/await and am running into a few issues.
I have this code:
private async void GetPages()
{
PageList = await _dataService.GetPageList(JobTypeId);
}
IDataService looks like this
public interface IDataService
{
Task<List<PageDto>> GetPageList(int jobTypeId);
}
and DataService.GetPageList like this:
public Task<List<PageDto>> GetPageList(int jobTypeId)
{
var retval = new List<PageDto>();
try
{
retval =
(from p in _connection.Table<PageDto>()
where p.JobTypeId == jobTypeId
select p).ToList();
}
catch (Exception ex)
{
Logger.Exception(ex);
}
return retval;
}
I somehow need to wrap the result in a task but I can't figure out how.
Any ideas?
UPDATE
I can't use ToListAsync due to limitations in the SQLLite library I'm using but how about something like this?
public async Task<List<PageDto>> GetPageListAsync(int jobTypeId)
{
var retval = new List<PageDto>();
try
{
await Task.Run(() =>
{
retval =
(from jtp in _connection.Table<JobTypePage>()
join p in _connection.Table<PageDto>() on jtp.PageId equals p.PageId
where jtp.JobTypeId == jobTypeId
orderby jtp.JobTypePageId
select p).ToList();
});
}
catch (Exception ex)
{
Logger.Exception(ex);
}
return retval;
}