public Task <BatchInfo> TryGetNextBatch() { if (Batches.All(x => x.State == BatchState.Completed)) { return(Task.FromResult <BatchInfo>(null)); } var stagedBatch = Batches.SingleOrDefault(x => x.State == BatchState.Staged); if (stagedBatch != null) { return(Task.FromResult <BatchInfo>(stagedBatch)); } var pendingBatch = Batches.First(x => x.State == BatchState.Pending); return(Task.FromResult <BatchInfo>(pendingBatch)); }