dc7a9163d9 dc7a9163d9 - 27 days ago 8
C# Question

Convert lambda type from int => IEnumerable<string>> to int => Task<IEnumerable<string>>>?

I have a method

string M1(Func<int x, Task<IEnumerable<string>>> getList)
// getList: int => Task<IEnumerable<string>>>
{ ...... }


How to make the parameter of M1 in the following to type
int => Task<IEnumerable<string>>>
.

var l = new List<string>(); .... // l is pre-built
var r = M1(x => l); // The type of param is int => IEnumerable<string>>


async x => l
will give a warning.

Answer

Use Task.FromResult() to create a task with a result already in the completed state.

var l = new List<string>(); .... // l is pre-built
var r = M1(x => Task.FromResult<IEnumerable<string>>(l));