private static async Task <ScheduledCommand> GetStoredScheduledCommand <TAggregate>( IScheduledCommand <TAggregate> scheduledCommand, CommandSchedulerDbContext db, Guid scheduledCommandGuid) where TAggregate : class { var sequenceNumber = scheduledCommand .IfTypeIs <IEvent>() .Then(c => c.SequenceNumber) .Else(() => scheduledCommand .IfTypeIs <ScheduledCommand <TAggregate> >() .Then(c => c.SequenceNumber)) .ElseThrow(() => new InvalidOperationException("Cannot look up stored scheduled command based on a " + scheduledCommand.GetType())); var storedCommand = await db.ScheduledCommands .SingleOrDefaultAsync( c => c.AggregateId == scheduledCommandGuid && c.SequenceNumber == sequenceNumber); return(storedCommand); }