问题
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)
isIEnumerable<int>
,TSource
isint
- Making the lambda async, taking an
int
and returning anint
should produce aTask<int>
, which is whatTResult
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