Home > Net >  How to convert Task<Object> to List<Object>
How to convert Task<Object> to List<Object>

Time:08-26

I am working on a legacy project.

public List<object> GetExternalDataSets(int dataSourceId)
{
    var dataSource = _context.DataSources.FirstOrDefault(x => x.DataSourceId == dataSourceId);
    if (dataSource != null)
    {
        if (dataSource.DataSourceType != "PowerBI")
        {
            var dbInfo = GetDataSourceInfo(dataSource);
            IExternalDbContext externalDbContext = ExternalContextFactory.GetExternalDbContext(dbInfo, config);
            return externalDbContext.GetAllTables(dbInfo.DBName);
        }
        else {
           //Part added by me
            return  powerBIService.GetPowerBIDatasetsAsync(dataSource.WorkspaceId);     
        }
    }
    return new List<object>();
}

I have added few new lines to it. But powerBIService.GetPowerBIDatasetsAsync(dataSource.WorkspaceId); is returning the type of Task<object>. How can I convert it to List<object>.

CodePudding user response:

By awaiting the task. Make your method async:

public async Task<List<object>> GetExternalDataSets(int dataSourceId)

And await the task:

return await powerBIService.GetPowerBIDatasetsAsync(dataSource.WorkspaceId);

Regarding your edit...

is returning the type of Task<object>. How can I convert it to List<object>

In that case you'd also have to add the awaited object to a List:

var obj = await powerBIService.GetPowerBIDatasetsAsync(dataSource.WorkspaceId);
return new List<object> { obj };
  •  Tags:  
  • c#
  • Related