sql - 如何使用 SqlQuerySpec 查询 CosmosDb 的多个结果
问题描述
我正在尝试使用数字列表获取多个文档。这是我的查询
var queryString = "Select * from c Where c.id in (@list)";
var queryParam = new Microsoft.Azure.Documents.SqlParameter("@list", string.Join(",", list.Select(x => $"{x.Id}").ToArray()));
var result = repo.Query(
new SqlQuerySpec()
{
QueryText = queryString,
Parameters = new Microsoft.Azure.Documents.SqlParameterCollection()
{
queryParam
}
}
);
我的查询功能:
public IQueryable<TClass> Query(SqlQuerySpec sqlQuerySpec = null, bool allowScan = false, int? maxItems = null)
{
var feedOptions = new FeedOptions
{
EnableScanInQuery = allowScan,
MaxItemCount = maxItems,
EnableCrossPartitionQuery = true
};
var querySpec = sqlQuerySpec ?? new SqlQuerySpec();
return sqlQuerySpec != null
? Client.CreateDocumentQuery<TClass>(Collection.DocumentsLink, querySpec, feedOptions)
: Client.CreateDocumentQuery<TClass>(Collection.DocumentsLink, feedOptions);
}
它说有一个错误:one of the specified inputs is invalid
。
我究竟做错了什么?
解决方案
这id
是一个字符串,因此您需要将每个 id 用单引号括起来。
这一行:
var queryParam = new Microsoft.Azure.Documents.SqlParameter("@list", string.Join(",", list.Select(x => $"{x.Id}").ToArray()));
应该:
var queryParam = new Microsoft.Azure.Documents.SqlParameter("@list", string.Join(",", list.Select(x => $"'{x.Id}'").ToArray()));
推荐阅读
- openshift - 多个 pod 上的 Openshift 3.11 堆转储 OOM
- python - 如何从另一个带有参数的python文件运行python文件?
- javascript - 如何在Angular中使用chart.js将对象值的JSON数组传递给堆叠图表?
- java - 如何在变量中添加 jTable 不同行的内容?
- node.js - 通过 proxy_pass 在 NGINX 中提供 NodeJS HTTPS 应用程序
- python - python在压缩时将.tar.gz流式传输到s3
- javascript - 从 JavaScript 代码中消除确认警报
- c - 使用指针的程序运行不好
- node.js - 数组不记录任何内容
- node.js - 如何快速执行嵌套 for 循环中编写的等待函数