如何从IEnumerable<T>上传I++table字典中的数据



我正在从 Azure 表存储中检索数据并将其存储在 IEnumerable 中,但我不知道如何将此IEnumerable<T>转换为IReliableDictionary<string, Store>,以便我可以将此存储数据保存到有状态服务的状态管理器中。

var storesRepo = await StateManager.GetOrAddAsync<IReliableDictionary<long, Store>>(""); 

那么如何将此IEnumerable<Store>插入storesRepo

店铺等级:

public class Store : TableEntity
{
public string Name { get; set; }
public double Longitude { get; set; }
public double Latitude { get; set; }
public string Address { get; set; }
public double Distance { get; set; }
public string Email { get; set; }
public string City { get; set; }
}

ReliableDictionary<string, Store>

其中键是存储名称。

  • 创建一个字典,枚举集合中的所有存储,在事务中逐个添加它们。
  • 确保商店集不会变得太大,以避免长时间运行的事务和锁定。
  • 确保先考虑分区
  • 添加重试支持。
  • 从此处应用最佳实践。

元代码如下:

var storesRepo = await StateManager.GetOrAddAsync<IReliableDictionary<string, Store>>("someName");    
IEnumerable<Store> stores = await GetFromTableStorage();
using (ITransaction tx = base.StateManager.CreateTransaction()) 
{
foreach(var store in stores)
{
await storesRepo.AddAsync(tx, store.Name, store, cancellationToken);
}
await tx.CommitAsync();
}

最新更新