无法隐式转换类型'System.Collections.Generic.List<>'到"System.Threading.Tasks.Task<>
问题描述:
我要例外了.
不能将类型
'System.Collections.Generic.List<IntegraPay.Domain.SObjects.Industry>'
隐式转换为'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;
}
答
当方法声明中缺少async
时,通常会发生此错误.
This error typically happens when you are missing async
in the method declaration.
将async
放在签名中时,C#编译器会添加魔术",以执行从对象到返回该对象的Task<T>
的转换.
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.
但是,根据您的情况,async
是不必要的,因为您返回的任务的结果已经是:
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
);