
AI
解决 Cosmos DB SQL API NOT IN 运算符采用 List WithParameter(List
csharpusing Microsoft.Azure.Cosmos;public static class CosmosDBQueryHelper{ public static SqlQuerySpec GenerateNotInQuerySpec<T>(string propertyName, List<T> valuesToExclude) { string query = $"SELECT * FROM c WHERE NOT udf.IsInArray(c.{propertyName}, @valuesToExclude)"; SqlParameterCollection parameters = new SqlParameterCollection(); parameters.Add(new SqlParameter("@valuesToExclude", valuesToExclude)); return new SqlQuerySpec(query, parameters); } public static bool IsInArray<T>(T value, IEnumerable<T> array) { return array.ContAIns(value); }}public class Document{ public string Id { get; set; } public string Name { get; set; }}public async Task<List<Document>> QueryDocumentsExcludingValues(List<string> valuesToExclude){ List<Document> results = new List<Document>(); using (CosmosClient client = new CosmosClient("connectionString")) { Database Database = awAIt client.GetDatabase("DatabaseId"); ContAIner contAIner = awAIt Database.GetcontAIner("contAInerId"); SqlQuerySpec querySpec = CosmosDBQueryHelper.GenerateNotInQuerySpec("Name", valuesToExclude); FeedIterator<Document> iterator = contAIner.GetItemQueryIterator<Document>(querySpec); while (iterator.HasMoreResults) { FeedResponse<Document> response = awAIt iterator.ReadNextAsync(); results.AddRange(response); } } return results;}在上面的示例代码中,我们创建了一个名为 CosmosDBQueryHelper 的辅助类,其中包含了一个生成查询规范的方法 GenerateNotInQuerySpec 和一个自定义函数 IsInArray。通过调用 GenerateNotInQuerySpec 方法,我们可以生成一个查询规范,该规范将使用自定义函数来模拟 NOT IN 运算符的行为。然后,我们可以使用生成的查询规范来执行查询,并获取结果。在使用 Cosmos DB SQL API 进行数据查询时,如果遇到 NOT IN 运算符无法正确处理列表参数的问题,我们可以使用自定义函数来模拟 NOT IN 运算符的行为。通过这种方式,我们可以正确地使用 NOT IN 运算符,并将列表参数传递给它。希望本文提供的解决方案能够帮助您解决这个问题,并顺利进行 Cosmos DB 数据查询。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号