Projection using async delegate/lambda

限于喜欢 提交于 2019-12-30 13:11:44

问题


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:


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



来源:https://stackoverflow.com/questions/10712655/projection-using-async-delegate-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!