我有一个Azure CosmosDB表,当我使用带有时间戳过滤器的Azure.Data.Tables C# SDK查询它时,它返回零列。当我删除筛选器时,它成功地应用了其余的查询。
例如,使用下面的过滤器:
Timestamp ge datetime'2022-11-04T22:24:14.851Z'当我在Azure Portal上应用相同的筛选器时,查询将按预期的方式运行,并根据时间戳属性筛选行。
我希望返回行,因为有满足此筛选器的行(它们是在此日期之后添加的)。
发布于 2022-11-15 12:22:55
使用下面的查询,您可以根据时间戳筛选结果。
"SELECT * FROM c WHERE (c[\"ShipDate\"] >= \"2014-09-30T23:14:25.7251173Z\")"我根据时间戳从cosmos db获取数据的步骤如下:
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:\t{item.name}");
}
} 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;
}
}

参考链接
https://stackoverflow.com/questions/74408815
复制相似问题