繁体   English   中英

如何使用 LINQ 对 Azure Cosmos Document DB SQL ZDB9474238D04ADE1 有效地进行动态查询

[英]How to effectively do dynamic query using LINQ against a Azure Cosmos Document DB SQL API?

I know that Azure Cosmos DB SQL API allows using SQL syntax to query the documents from it and working for me.

例如: SELECT * FROM c WHERE c.DynamicContent.MailingAddress.City LIKE '%PORT%'

我试图保存的数据是这样的:

{
  "Id": 1001,
  "AttentionName": "Keyword list",
  "DynamicContent": {
    "Agency": 2,
    "EnteredOn": "2001-03-30T16:20:00+0000",
    "UpdatedOn": "2001-03-30T16:20:00+0000",
    "Name": "Sample Name",
    "MailingAddress": {
      "Address1": "501 Abbey St",
      "City": "Portland",
      "StateProvince": "OR",
      "PostalCode": "97215",
      "Country": "United States"
    }
  }
}

由于 DynamicContent 中的内容是非结构化和动态的,我使用下面的 class 来保存数据:

public partial class CosmosDocument
{
    [JsonProperty("Id")]
    public long Id { get; set; }

    [JsonProperty("AttentionName")]
    public string AttentionName { get; set; }

    [JsonProperty("DynamicContent")]
    public dynamic DynamicContent { get; set; }
} 

我面临的问题是无法做这样的事情:

query.Where(c => c.DynamicContent.MailingAddress.City.Contains("PORT")).ToList();

因为过滤器中可能出现的属性(动态和 UI 控制)位于动态 class 中,并且不是强类型的。

有没有办法正确地做到这一点,或者至少有一些其他的解决方法来实现这一点? 请帮助您的想法。

与其使用 LINQ 进行查询,不如考虑使用允许从任意字符串和参数值构造的QueryDefinition GetItemQueryIterator文档中有一些示例,例如:

QueryDefinition queryDefinition = new QueryDefinition("select * from ToDos t where t.cost > @expensive")
    .WithParameter("@expensive", 9000);
using (FeedIterator<ToDoActivity> feedIterator = this.Container.GetItemQueryIterator<ToDoActivity>(
    queryDefinition,
    null,
    new QueryRequestOptions() { PartitionKey = new PartitionKey("Error")}))
{
    while (feedIterator.HasMoreResults)
    {
        foreach(var item in await feedIterator.ReadNextAsync())
        {
            Console.WriteLine(item.cost); 
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM