我有几种方法都结束了:
while (await cursor.MoveNextAsync())
{
foreach (FieldServiceAppointment appointment in cursor.Current)
{
yield return appointment;
}
}
例如:
public async IAsyncEnumerable<FieldServiceAppointment> GetEventWithWorkOrderType(
string workOrderType, string customerId)
{
using IAsyncCursor<FieldServiceAppointment> cursor = await FieldServiceAppointments.FindAsync(
x => x.BusinessEventTypeCode == workOrderType
&& x.CustomerCode == customerId
);
while (await cursor.MoveNextAsync())
{
foreach (FieldServiceAppointment appointment in cursor.Current)
{
yield return appointment;
}
}
}
我想删除这个重复。
如果我尝试将其重构为:
public async IAsyncEnumerable<FieldServiceAppointment> GetEventWithWorkOrderType(
string workOrderType, string customerId)
{
using IAsyncCursor<FieldServiceAppointment> cursor = await FieldServiceAppointments.FindAsync(
x => x.BusinessEventTypeCode == workOrderType
&& x.CustomerCode == customerId
);
YieldAppointments(cursor);
}
public async IAsyncEnumerable<FieldServiceAppointment> YieldAppointments(
IAsyncCursor<FieldServiceAppointment> cursor)
{
while (await cursor.MoveNextAsync())
{
foreach (FieldServiceAppointment appointment in cursor.Current)
{
yield return appointment;
}
}
}
它不会编译,因为我无法从迭代器返回值。
如果我尝试 return yield return YieldAppointments(cursor);
,它将无法编译,因为:
严重性代码描述项目文件行抑制状态错误 CS0266 无法将类型“System.Collections.Generic.IAsyncEnumerable<DataAccessLayer.Entities.Praxedo.FieldServiceAppointment>”隐式转换为“DataAccessLayer.Entities.Praxedo.FieldServiceAppointment”。存在显式转换(您是否缺少演员表?) DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService.cs 78 Active
所以我试图
yield return (IAsyncEnumerable<FieldServiceAppointment>) YieldAppointments(cursor);
和
yield return YieldAppointments(cursor) as IAsyncEnumerable <FieldServiceAppointment>;
其中任何一个都会产生以下编译器错误:
严重性代码描述项目文件行抑制状态错误 CS0266 无法将类型“System.Collections.Generic.IAsyncEnumerable<DataAccessLayer.Entities.Praxedo.FieldServiceAppointment>”隐式转换为“DataAccessLayer.Entities.Praxedo.FieldServiceAppointment”。存在显式转换(您是否缺少演员表?) DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService.cs 78 Active
所以我尝试了:
public async IAsyncEnumerable<FieldServiceAppointment> GetEventWithWorkOrderType(
string workOrderType, string customerId)
{
using IAsyncCursor<FieldServiceAppointment> cursor = await FieldServiceAppointments.FindAsync(
x => x.BusinessEventTypeCode == workOrderType
&& x.CustomerCode == customerId
);
yield return await YieldAppointments(cursor);
}
public async Task<FieldServiceAppointment> YieldAppointments(
IAsyncCursor<FieldServiceAppointment> cursor)
{
while (await cursor.MoveNextAsync())
{
foreach (FieldServiceAppointment appointment in cursor.Current)
{
yield return appointment;
}
}
}
但这不会编译,因为
严重性代码描述项目文件行抑制状态错误 CS1624“FieldServiceAutomationDbService.YieldAppointments(IAsyncCursor)”的主体不能是迭代器块,因为“任务”不是迭代器接口类型 DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService。 cs 81 主动
有没有办法使这项工作?