【问题标题】:Azure.Data.Tables C# SDK query Timestamp returning zero resultsAzure.Data.Tables C# SDK 查询时间戳返回零结果
【发布时间】:2023-01-27 17:21:59
【问题描述】:

我有一个 Azure CosmosDB 表,当我使用带有时间戳筛选器的 Azure.Data.Tables C# SDK 查询它时,它返回零列。当我删除过滤器时,它会成功应用剩余的查询。

例如,使用下面的过滤器:

Timestamp ge datetime'2022-11-04T22:24:14.851Z'

当我在 Azure 门户上应用相同的筛选器时,查询的行为符合预期,并根据 Timestamp 属性筛选行。

我期待返回行,因为有满足此过滤器的行(它们是在此日期之后添加的)。

【问题讨论】:

    标签: c# azure


    【解决方案1】:

    使用以下查询,您可以根据时间戳过滤结果。

    "SELECT * FROM c WHERE (c["ShipDate"] >= "2014-09-30T23:14:25.7251173Z")"
    

    我根据时间戳从 cosmos db 获取数据所遵循的步骤是,

    1. 在 Azure 中创建了 cosmos 数据库。
    2. 创建了一个 C# 控制台应用程序并添加了以下代码,
      using Microsoft.Azure.Cosmos;
      using Microsoft.Azure.Cosmos.Linq;
      // </using_directives>
      
      // <endpoint_key> 
      // New instance of CosmosClient class using an endpoint and key string
      var Endpoint = "<cosmos db url>";
      var Key = "<yourkey>";
      CosmosClient cosmosClient = new CosmosClient(Endpoint, Key);// </endpoint_key>
      
      // <create_database>
      // New instance of Database class referencing the server-side database
      Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync(
          id: "adventureworks"
      );
      // </create_database>
      
      // <create_container>
      // New instance of Container class referencing the server-side container
      Container container = await database.CreateContainerIfNotExistsAsync(
          id: "products",
          partitionKeyPath: "/category",
          throughput: 400
      );
      // </create_container>
      
      // <create_items> 
      // Create new items and add to container
      Product firstNewItem = new(
          id: "68719518388",
          category: "gear-surf-surfboards",
          name: "Sunnox Surfboard",
          quantity: 8,
          sale: true,
          ShipDate: DateTime.UtcNow.AddDays(-14)
      );
      
      Product secondNewitem = new(
          id: "68719518398",
          category: "gear-surf-surfboards",
          name: "Noosa Surfboard",
          quantity: 15,
          sale: false,
          ShipDate: DateTime.UtcNow.AddDays(-10)
      );
      
      await container.CreateItemAsync<Product>(
          item: firstNewItem,
          partitionKey: new PartitionKey("gear-surf-surfboards")
      );
      
      await container.CreateItemAsync<Product>(
          item: secondNewitem,
          partitionKey: new PartitionKey("gear-surf-surfboards")
      );
      // </create_items> 
      
      // <query_items_sql>
      // Query multiple items from container
      using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
          queryText: "SELECT * FROM c WHERE (c["ShipDate"] >= "2014-09-30T23:14:25.7251173Z")"
      );
      
      // Iterate query result pages
      while (feed.HasMoreResults)
      {
          FeedResponse<Product> response = await feed.ReadNextAsync();
      
          // Iterate query results
          foreach (Product item in response)
          {
              Console.WriteLine($"Found item:	{item.name}");
          }
      }
      
      1. 添加了一个名为 Product.cs 的类并添加了以下代码,
       class Product
      {
          public string id;
          public string category;
          public string name;
          public int quantity;
          public bool sale;
          public DateTime ShipDate;
      
          public Product(string id, string category, string name, int quantity, bool sale,DateTime ShipDate)
          {
              this.id = id;
              this.category = category;
              this.name = name;
              this.quantity = quantity;
              this.sale = sale;
              this.ShipDate = ShipDate;
      
          }   
      }
      
      1. 能够从 cosmos db 获取基于时间戳过滤条件的行。

      参考link

    【讨论】:

      【解决方案2】:

      显然这是 Azure.Data.Tables SDK 的一个特性:

      https://github.com/Azure/azure-sdk-for-net/issues/32468

      【讨论】:

        猜你喜欢
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 2013-04-29
        • 1970-01-01
        • 2016-03-09
        相关资源
        最近更新 更多