I have several methods which all end:
while (await cursor.MoveNextAsync())
{
foreach (FieldServiceAppointment appointment in cursor.Current)
{
yield return appointment;
}
}
For example:
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;
}
}
}
I'd like to remove this duplication.
If I try to refactor it to this:
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;
}
}
}
It won't compile because I can't return a value from an iterator.
If I try to return yield return YieldAppointments(cursor);
, it won't compile because:
Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IAsyncEnumerable<DataAccessLayer.Entities.Praxedo.FieldServiceAppointment>' to 'DataAccessLayer.Entities.Praxedo.FieldServiceAppointment'. An explicit conversion exists (are you missing a cast?) DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService.cs 78 Active
So I tried to
yield return (IAsyncEnumerable<FieldServiceAppointment>) YieldAppointments(cursor);
and
yield return YieldAppointments(cursor) as IAsyncEnumerable <FieldServiceAppointment>;
either of which generate a compiler error of:
Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IAsyncEnumerable<DataAccessLayer.Entities.Praxedo.FieldServiceAppointment>' to 'DataAccessLayer.Entities.Praxedo.FieldServiceAppointment'. An explicit conversion exists (are you missing a cast?) DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService.cs 78 Active
So then I tried:
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;
}
}
}
but this won't compile because
Severity Code Description Project File Line Suppression State Error CS1624 The body of 'FieldServiceAutomationDbService.YieldAppointments(IAsyncCursor)' cannot be an iterator block because 'Task' is not an iterator interface type DataAccessLayer C:\projects\EnpalPraxedoIntegration\DataAccessLayer\DbServices\FieldServiceAutomationDbService.cs 81 Active
Is there a way to make this work?