我正在寻找从Cosmos Db集合中检索的项目,并且我有一个要检索的ID列表。所有代码都在Azure函数中运行,下面是在列表 filteredResult 中获取所有ID的代码。我正在寻找完成此代码的最佳方法,以一次考虑30-40个ID来从Cosmos Db集合中检索所有项目。
public static void Run([ServiceBusTrigger("testSB", "SubscriberName", Connection = "AzureServiceBusString")] string mySbMsg,
[CosmosDB(
databaseName: "DBName",
collectionName: "CollectionName",
ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client,
ILogger log)
{
try {
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
List<MyItem> lists = JsonConvert.DeserializeObject<List<MyItem>>(mySbMsg, jsonSerializerSettings);
List<string> filteredResult = (from s in lists
where s.DocType == "TEST"
select s.Id).ToList();
}
答案 0 :(得分:1)
该问题最近在Microsoft Q&A forum中得到了介绍。 在此处再次发布答案以扩大覆盖范围。
在创建“ filteredResult”(只需将“ input”列表替换为“ filteredResult”之后)即可使用此代码
List<string> input = new List<string>();
input.Add("1");
input.Add("2");
input.Add("3");
var option = new FeedOptions { EnableCrossPartitionQuery = true };
IQueryable<Family> queryable = client.CreateDocumentQuery<Family>(UriFactory.CreateDocumentCollectionUri("families", "items").ToString(), "SELECT * FROM books where books.id IN " + "('" + string.Join( "','", input) + "')",option);
List<Family> posts = queryable.ToList();
Console.WriteLine("Read count = {0}", posts.Count);
还要注意,我为文档属性创建了一个模型类,如下所示:
public class Family
{
public int id;
public string city;
}