Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

async-await

I am getting an exception.

Cannot implicitly convert type 'System.Collections.Generic.List<IntegraPay.Domain.SObjects.Industry>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<IntegraPay.Domain.SObjects.Industry>>'

Below is my property and method.

  private List<WebFormFieldContent> WebFormFields { get; set; } = 
       new List<WebFormFieldContent>();

  Task<IEnumerable<WebFormFieldContent>> IRegistrationRepository.GetWebFormFields()
        {
            return WebFormFields;
        }
like image 381
maxspan Avatar asked Jul 26 '16 00:07

maxspan


1 Answers

This error typically happens when you are missing async in the method declaration.

When you put async in the signature, C# compiler adds "magic" to do the conversion from an object to a Task<T> returning that object.

However, in your situation async is unnecessary, because you return a task with a result that you already have:

return Task.FromResult<IEnumerable<WebFormFieldContent>>(
    WebFormFields
);
like image 68
Sergey Kalinichenko Avatar answered Oct 26 '22 04:10

Sergey Kalinichenko