linq - Azure 函数 CosmosDB 查询序列化

标签 linq azure azure-cosmosdb azure-functions

我在使用 Azure 函数和 DocumentClient 序列化 linq 查询时遇到问题。该查询不使用我的 POCO 的 JsonProperty 属性。

linq 查询返回 {{"query":"SELECT * FROM root WHERE (root[\"ObjectType\"] =\"Campaign\") "}} 而不是 {{"query":"SELECT * FROM root WHERE (root[\"objectType\"] =\"Campaign\") "}} linq 查询和 POCO

var query = client.CreateDocumentQuery<Obj>(UriFactory.CreateDocumentCollectionUri("db", "col"))
                            .Where(d => d.ObjectType == "MyObj")
                            .AsEnumerable();
public class Obj
{
   [Newtonsoft.Json.JsonProperty("objectType")]
   public string ObjectType { get; set; }
}

azure 函数是使用 azure-functions-core-tools 启动的预编译函数。

我的开发环境是:

  • VS 2017
  • azure-functions-core-tools(最新)
  • 网络4.6.1
  • DocumentDB SDK:1.14.0
  • 牛顿软件:10.0.0

相同的代码在 iisexpress 中运行时效果很好。

感谢您的帮助!

最佳答案

我无法重现这一点。有这个功能

public static class HttpTriggerCSharp
{
    [FunctionName("HttpTriggerCSharp")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger()] HttpRequestMessage req, TraceWriter log)
    {
        var client = new DocumentClient(new Uri("https://example.com"), string.Empty);
        var query = client.CreateDocumentQuery<Obj>(UriFactory.CreateDocumentCollectionUri("db", "col"))
                        .Where(d => d.ObjectType == "MyObj")
                        .ToString();
        log.Info(query);
        return req.CreateResponse(HttpStatusCode.OK, "OK");
    }
}

public class Obj
{
    [Newtonsoft.Json.JsonProperty("objectType")]
    public string ObjectType { get; set; }
}

正确打印 {"query":"SELECT * FROM root WHERE (root[\"objectType\"] =\"MyObj\") "}

你能尝试一下吗?

这些是我的 csproj 中的所有软件包

<PackageReference Include="Microsoft.Azure.DocumentDB" Version="1.14.1" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="2.1.0-beta1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="1.0.0-beta1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.0-alpha5" />

关于linq - Azure 函数 CosmosDB 查询序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453721/

相关文章:

linq - Entity Framework - "Unable to create a constant value of type ' 闭包类型'...”错误

c# - 转换为 LINQ 查询的 IEnumerable<DateTime> 结果

azure - Azure 前门服务中的缓存

python - 如何测试使用 Context 的 Azure Function?

azure - 在 Foreach 内部 append 变量 : Azure data factory

asp.net - 值不能为空。参数名称: uriString

azure - 错误: The order by query does not have a corresponding composite index that it can be served from

c# - 如何使用 ITranscriptLogger 和 TranscriptLoggerMiddleware 在 cosmos DB 中存储聊天记录

c# - 我可以将此 C# 代码转换为一些 Linq 代码吗?

asp.net - 基于 ASP.NET 的网站的数据库建议和最佳实践?