在 Azure 函数的 DocumentDB 属性中发送 SqlQuery
Send SqlQuery in Azure Function's DocumentDB Attribute
我有一个使用 DocumentDB
属性连接到 Cosmos DB 的 Azure 函数。我正在使用 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
我收到的 "error" 是它无法识别任何名为 SqlQuery
的可能参数。
我查看了 Azure 函数输入绑定的文档
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents 显示 function.json
文件中包含 sqlQuery
属性的输出。那是怎么进来的?
如果无法在 DocumentDB
属性中传递 SqlQuery,最好的做法是预先过滤结果以避免返回整个集合然后 运行通过 LINQ 查询?
您需要参考 1.1.0-beta
版本的 Microsoft.Azure.WebJobs.Extensions.DocumentDB
NuGet 包(或更高版本)。
在该版本中 SqlQuery
是 DocumentDB
属性的有效参数。如果我在 select
string:
之前删除 $
符号,你的代码会为我编译
[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]
您不需要 $
- 它用于 C# 中的字符串插值,而不是您想在此处执行的操作。
我有一个使用 DocumentDB
属性连接到 Cosmos DB 的 Azure 函数。我正在使用 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
我收到的 "error" 是它无法识别任何名为 SqlQuery
的可能参数。
我查看了 Azure 函数输入绑定的文档
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-documentdb#input-sample-with-multiple-documents 显示 function.json
文件中包含 sqlQuery
属性的输出。那是怎么进来的?
如果无法在 DocumentDB
属性中传递 SqlQuery,最好的做法是预先过滤结果以避免返回整个集合然后 运行通过 LINQ 查询?
您需要参考 1.1.0-beta
版本的 Microsoft.Azure.WebJobs.Extensions.DocumentDB
NuGet 包(或更高版本)。
在该版本中 SqlQuery
是 DocumentDB
属性的有效参数。如果我在 select
string:
$
符号,你的代码会为我编译
[DocumentDB("FunctionJunctionDemo", "Demo", SqlQuery = "select * from c where c.Id = {Id}")]
您不需要 $
- 它用于 C# 中的字符串插值,而不是您想在此处执行的操作。