Answers:
当然-您只需要该ToListAsync()
方法,该方法位于System.Linq.Async
NuGet包中。这是一个完整的示例:
项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Linq.Async" Version="4.0.0" />
</ItemGroup>
</Project>
码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
IAsyncEnumerable<string> sequence = GetStringsAsync();
List<string> list = await sequence.ToListAsync();
Console.WriteLine(list.Count);
}
static async IAsyncEnumerable<string> GetStringsAsync()
{
yield return "first";
await Task.Delay(1000);
yield return "second";
await Task.Delay(1000);
yield return "third";
}
}
await iOrderedQueryable.ToListAsync(cancellationToken)
添加nuget软件包后失败。我删除了nuget包后,错误消失了。