Home > Software design >  How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>?
How can I convert from a Task<Generic.List<T>> to a Generic.IEnumerable<T>?

Time:12-16

This is my parent component:

private IEnumerable<InventoryListDTO> InventorySelected { get; set; } = new List<InventoryListDTO>();

InventorySelected = _inventoryRepository.GetForList(inventorySelectDTO);

This is my implementation:

public async Task<List<InventoryListDTO>> GetForList(T_IFS_InventorySelectDTO InventorySelection)

And this is what is returned:

IEnumerable<InventoryListDTO> itemsMapped = _mapper.Map<IEnumerable<T_IFS_Inventory>, IEnumerable<InventoryListDTO>>(query);
{ async calls to modify data in itemsMapped }
return itemsMapped.ToList();

I need the implementation to be async. Maybe I am not using the correct types but the error I am getting is:

Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<ISInventory_Models.InventoryListDTO>>' to 'System.Collections.Generic.IEnumerable<ISInventory_Models.InventoryListDTO>'

I'm fine with returning an IEnumerable or a List but it must be async. I had this working until I tried to make it async and now I'm pretty confused. I have tried too many things to list but I can't quite get it right and I was hoping someone could assist. Please comment if you need additional information. Thanks in advance.

CodePudding user response:

You need to await the Task like this within the context of an async method. In your case probably OnInitializedAsync as it's in a component:

private IEnumerable<InventoryListDTO> InventorySelected = Enumerable.Empty<InventoryListDTO>();

protected async override Task OnInitializedAsync()
{
   InventorySelected = await _inventoryRepository.GetForList(inventorySelectDTO);
}

You can also assign an empty Enumerable on initialization rather that building out a List.

  • Related