我正在尝试通过documentClientHelper查询宇宙。我有在查询资源管理器中运行良好的查询,但是使用带有参数的相同查询,将错误抛出到连接处。
错误显示为-
错误:
Microsoft.Azure.DocumentDB.Core:消息: {“错误”:[{“严重性”:“错误”,“位置”:{“开始”:428,“结束”:432},“代码”:“ SC1001”,“消息”:“语法” 错误,“联接”附近的语法不正确。“}]}
我的查询如下:
SELECT
c.itemCode,c.itemType,c.name,r
FROM Products c join r in c.rates
WHERE (ARRAY_CONTAINS(r.branchCodes, 80171, true)) AND (c.itemCode = "0600160")
ORDER BY c.name
以下部分的以下查询失败:
var queryText = "SELECT
c.itemCode,c.itemType,c.name,r
FROM Products c join r in c.rates
WHERE (ARRAY_CONTAINS(r.branchCodes, 80171, true)) AND (c.itemCode = "0600160")
ORDER BY c.name";
var querySpec = new SqlQuerySpec(queryText);
var query = documentClient.CreateDocumentQuery<Product>(collectionUri, querySpec, feedOptions).AsDocumentQuery();
var response = await DocumentClientHelper.ExecuteWithRetries(async () => await query.ExecuteNextAsync<Product>());
答案 0 :(得分:0)
根据我的测试,两种方法都可以正常工作。
样本数据:
[
{
"name": "aa",
"rates": [
{
"branchCodes": [
80171,
80172,
80170
]
}
]
},
{
"name": "aa",
"rates": [
{
"branchCodes": [
80170
]
}
]
},
{
"name": "aa",
"rates": [
{
"branchCodes": [
80172
]
}
]
}
]
在门户网站中查询sql:
SELECT
c.itemCode,c.itemType,c.name,r
FROM Products c join r in c.rates
WHERE (ARRAY_CONTAINS(r.branchCodes, 80171, true)) AND (c.name = "aa")
ORDER BY c.name
带有SqlQuerySpec
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);
String sql = "SELECT c.name,r " +
"FROM Products c join r in c.rates "+
" WHERE(ARRAY_CONTAINS(r.branchCodes, 80171, true)) AND(c.name = \"aa\") " +
" ORDER BY c.name ";
IQueryable<Pojo> queryable = client.CreateDocumentQuery<Pojo>(
uri,
new SqlQuerySpec
{
QueryText = sql
});
foreach (Pojo p in queryable)
{
Console.WriteLine("\nRead {0}", p);
}
}
}
class Pojo : Document
{
public string id { get; set; }
public string name { get; set; }
}
}