Projection using async delegate/lambda

2019-02-20 14:13发布

The following code will not compile against the Async CTP in Visual Studio 2010:

Enumerable.Range(1, 5).Select(async x =>
{
    await TaskEx.Delay(100);
    return 5;
});

The compilation error is as follows:

Test.cs(40,13): error CS1928: 'System.Collections.Generic.IEnumerable<int>' does not contain a definition for 'Select' and the best extension method overload 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' has some invalid arguments
Test.cs(40,49): error CS1503: Argument 2: cannot convert from 'lambda expression' to 'System.Func<int,int>'

However, by my read, the following should occur here:

  • The overload IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector) should be used.
  • As the .Range(1, 5) is IEnumerable<int>, TSource is int
  • Making the lambda async, taking an int and returning an int should produce a Task<int>, which is what TResult should be.

I don't see a syntax issue here. What is the problem?

Note that I cannot use VS11 yet because my main application requires Azure tools.

EDIT: This works fine with msbuild at the command line, but not in VS2010. It seems that at the command line, the VS11 compiler is being used even though I'm targeting .NET 4.0, whereas VS2010 uses its own in-process compiler. Does anyone know how to swap out the compiler that VS2010 uses?

1条回答
小情绪 Triste *
2楼-- · 2019-02-20 15:05

Works in my LINQPad (VS11 Beta installed on the machine)

enter image description here

查看更多
登录 后发表回答