我正在尝试使用包含以下内容的查询文本创建SqlQuerySpec类:
is_defined (@param)
,我很难用@param值创建相应的SqlParameter类。它始终表示为字符串,而不是json文档路径。
示例:
Select *
from c ....
where is_defined("some_json_document_path")
代替:
Select *
from c ....
where is_defined(some_json_document_path)
答案 0 :(得分:1)
只需稍微修改一下代码即可。
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Linq;
namespace JayGongDocumentDB.module
{
class QueryWithSqlParam
{
private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
private static readonly string authorizationKey = "***";
private static readonly string databaseId = "db";
private static readonly string collectionId = "coll";
private static DocumentClient client;
public static async void QueryTest()
{
client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
var Parameters = new SqlParameterCollection()
{
new SqlParameter("@param", "c.test")
};
IQueryable<Pojo> queryable = client.CreateDocumentQuery<Pojo>(
uri,
new SqlQuerySpec
{
QueryText = "SELECT c.id,c.name,c.test FROM c WHERE is_defined(" + Parameters[0].Value + ")"
});
foreach (Pojo p in queryable)
{
Console.WriteLine("\nRead {0}", p);
}
}
}
class Pojo : Document
{
public string id { get; set; }
public string name { get; set; }
}
}
示例代码:
[
{
"id": "1",
"name": "Jay"
},
{
"id": "2",
"name": "Bob",
"test": []
},
{
"id": "3",
"name": "Jay"
}
]
输出:
希望它对您有帮助。