Skip to content

Instantly share code, notes, and snippets.

@PradeepLoganathan
Last active November 13, 2020 05:58
Show Gist options
  • Save PradeepLoganathan/3c7f9452eb3b8aa9c38c324b9650db3f to your computer and use it in GitHub Desktop.
Save PradeepLoganathan/3c7f9452eb3b8aa9c38c324b9650db3f to your computer and use it in GitHub Desktop.
public async Task AddOrders(List<Order> orders, string customerId)
{
var ordersToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
foreach (var order in orders)
{
var stream = new MemoryStream();
await JsonSerializer.SerializeAsync(stream, order);
ordersToInsert.Add(new KeyValuePair<PartitionKey, Stream>(new PartitionKey(order.CustomerId), stream));
}
var parallelTasks = new List<Task>();
foreach (var (key, value) in ordersToInsert)
{
parallelTasks.Add(this._cosmosDbContext.OrdersContainer.CreateItemStreamAsync(value, key)
.ContinueWith(x =>
{
var response = x.Result;
Console.WriteLine($"Bulk insert {response.ClientRequestId} has status {response.Status} with message {response}");
}));
}
await Task.WhenAll(parallelTasks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment