我有一个 Azure 函数,它使用 DocumentDB
属性连接到 Cosmos DB。我正在使用 Azure Functions 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);
}
我只见过 1 个示例这样做,并报告说它应该有效。 https://github.com/Azure/Azure-Functions/issues/271
我收到的“错误”是它无法将任何名为 SqlQuery
的内容识别为可能的参数。
我查看了 Azure 函数输入绑定(bind)的文档
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents它显示了 function.json
文件中包含 sqlQuery
属性的输出。它是怎么进来的?
如果无法在 DocumentDB
属性中传递 SqlQuery,那么预先过滤结果以避免返回整个集合然后通过LINQ 查询?
最佳答案
您需要引用 Microsoft.Azure.WebJobs.Extensions.DocumentDB
NuGet 包的 1.1.0-beta
版本(或更高版本)。
在该版本中,SqlQuery
是 DocumentDB
属性的有效参数。如果我在 select
字符串之前删除 $
符号,您的代码就会为我编译:
[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]
您不需要 $
- 它用于 C# 中的字符串插值,而不是您想要在此处执行的操作。
关于azure - 在Azure函数的DocumentDB属性中发送SqlQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471234/