我有一个使用DocumentDB
属性连接到Cosmos DB的Azure功能。我正在使用Azure函数for Visual Studio 2017工具。这是简单的函数
[FunctionName("DocumentDbGet")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
[DocumentDB("FunctionJunctionDemo", "Demo")]IEnumerable<DemoModel> items)
{
//Can perform operations on the "items" variable, which will be the result of the table/collection you specify
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
var item = items.Where(x => x.FirstName == name);
return req.CreateResponse(HttpStatusCode.OK);
}
我希望能够将SqlQuery
作为DocumentDB
属性的一个参数传递,如下所示:
[FunctionName("DocumentDbGet")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage req, TraceWriter log,
[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = $"select * from c where c.Id = {Id}")]IEnumerable<DemoModel> items)
{
//Can perform operations on the "items" variable, which will be the result of the table/collection you specify
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
var item = items.Where(x => x.FirstName == name);
return req.CreateResponse(HttpStatusCode.OK);
}
我只看到一个例子这样做,并报告它应该工作。 https://github.com/Azure/Azure-Functions/issues/271
我收到的“错误”是它无法识别任何名为SqlQuery
的可能参数。
我查看了Azure Function Input绑定的文档
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents,其中显示function.json
文件中包含sqlQuery
属性的输出。那是怎么进入的?
如果无法在DocumentDB
属性中传入SqlQuery,那么最好先过滤结果以避免返回整个集合然后通过LINQ查询运行它?
答案 0 :(得分:5)
您需要引用1.1.0-beta
版本的Microsoft.Azure.WebJobs.Extensions.DocumentDB
NuGet包(或更高版本)。
在该版本中,SqlQuery
是DocumentDB
属性的有效参数。如果我在$
字符串之前移除select
符号,则为我编码编译:
[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]
你不需要$
- 它用于C#中的字符串插值,而不是你想在这里做的事情。