如何在存储库 class 中使用 IAsyncEnumerable
How to use IAsyncEnumerable in repository class
我正在使用 .net core 3.1 和 EF core 创建一个小型 API。
我正在尝试在我的存储库中使用 IAsyncEnumerable class,但出现错误。
我知道错误是有效的,但有人能告诉我如何在存储库 class 中使用 IAsyncEnumerable 吗?
StateRepository.cs
public class StateRepository : IStateRepository
{
public StateRepository(AssetDbContext dbContext)
: base(dbContext)
{
}
public async Task<State> GetStateByIdAsync(Guid id)
=> await _dbContext.States
.Include(s => s.Country)
.FirstOrDefaultAsync(s => s.StateId == id);
public async IAsyncEnumerable<State> GetStates()
{
// Error says:
//cannot return a value from iterator.
//Use the yield return statement to return a value, or yield break to end the iteration
return await _dbContext.States
.Include(s => s.Country)
.ToListAsync();
}
}
谁能告诉我哪里出错了?
谢谢
IAsyncEnumerable 与您想象的不同。
IAsyncEnumerable 在使用“yield”关键字的异步方法中使用。 IAsyncEnumerbale 允许它 return 每个项目一项一项。例如,如果您正在从事物联网方面的工作,并且希望在结果出现时“流式传输”它们。
static async IAsyncEnumerable<int> FetchIOTData()
{
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);//Simulate waiting for data to come through.
yield return i;
}
}
如果您对 IAsyncEnumerable 更感兴趣,可以在此处阅读更多内容:https://dotnetcoretutorials.com/2019/01/09/iasyncenumerable-in-c-8/
在您的情况下,您没有使用 Yield,因为您从一开始就拥有整个列表。您只需要使用常规的旧任务即可。例如:
public async Task<IEnumerable<<State>> GetStates()
{
// Error says:
//cannot return a value from iterator.
//Use the yield return statement to return a value, or yield break to end the iteration
return await _dbContext.States
.Include(s => s.Country)
.ToListAsync();
}
如果您正在调用某个服务 return 一个一个地编辑状态 并且 您想要一个一个地读取这些状态,那么您将使用 IAsyncEnumerable。但是对于您给出的示例(坦率地说,大多数用例),您只需使用 Task
就可以了
我正在使用 .net core 3.1 和 EF core 创建一个小型 API。
我正在尝试在我的存储库中使用 IAsyncEnumerable class,但出现错误。
我知道错误是有效的,但有人能告诉我如何在存储库 class 中使用 IAsyncEnumerable 吗?
StateRepository.cs
public class StateRepository : IStateRepository
{
public StateRepository(AssetDbContext dbContext)
: base(dbContext)
{
}
public async Task<State> GetStateByIdAsync(Guid id)
=> await _dbContext.States
.Include(s => s.Country)
.FirstOrDefaultAsync(s => s.StateId == id);
public async IAsyncEnumerable<State> GetStates()
{
// Error says:
//cannot return a value from iterator.
//Use the yield return statement to return a value, or yield break to end the iteration
return await _dbContext.States
.Include(s => s.Country)
.ToListAsync();
}
}
谁能告诉我哪里出错了?
谢谢
IAsyncEnumerable 与您想象的不同。
IAsyncEnumerable 在使用“yield”关键字的异步方法中使用。 IAsyncEnumerbale 允许它 return 每个项目一项一项。例如,如果您正在从事物联网方面的工作,并且希望在结果出现时“流式传输”它们。
static async IAsyncEnumerable<int> FetchIOTData()
{
for (int i = 1; i <= 10; i++)
{
await Task.Delay(1000);//Simulate waiting for data to come through.
yield return i;
}
}
如果您对 IAsyncEnumerable 更感兴趣,可以在此处阅读更多内容:https://dotnetcoretutorials.com/2019/01/09/iasyncenumerable-in-c-8/
在您的情况下,您没有使用 Yield,因为您从一开始就拥有整个列表。您只需要使用常规的旧任务即可。例如:
public async Task<IEnumerable<<State>> GetStates()
{
// Error says:
//cannot return a value from iterator.
//Use the yield return statement to return a value, or yield break to end the iteration
return await _dbContext.States
.Include(s => s.Country)
.ToListAsync();
}
如果您正在调用某个服务 return 一个一个地编辑状态 并且 您想要一个一个地读取这些状态,那么您将使用 IAsyncEnumerable。但是对于您给出的示例(坦率地说,大多数用例),您只需使用 Task
就可以了