Timestamp query in Azure

流过昼夜 提交于 2019-11-29 04:08:16

I am able to use the Timestamp with Microsoft.WindowsAzure.Storage version 4.0.1.0. Just and example

var query = TableQuery.CombineFilters(
            TableQuery.GenerateFilterCondition("Level", QueryComparisons.Equal, "ERROR"), 
            TableOperators.And, 
            TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, DateTimeOffset.Now.AddDays(-20).Date));

var query2 = TableQuery.CombineFilters(query,
            TableOperators.And, 
            TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, DateTimeOffset.Now));

var exQuery = new TableQuery<LogEntry>().Where(query2);

CloudTableClient tableClient = _storageAccount.CreateCloudTableClient();

CloudTable table = tableClient.GetTableReference(_tableName);
var results = table.ExecuteQuery(exQuery).Select(ent => (T) ent).ToList();

Property names are case-sensitive. It should be Timestamp and not TimeStamp.

Timestamp can be used to query rows in Azure Table Storage.

However, you will need to use TableQuery.GenerateFilterConditionForDate instead of TableQuery.GenerateFilterCondition. For any DateTime column, TableQuery.GenerateFilterConditionForDate must be used.

From your code sample, change GenerateFilterCondition to GenerateFilterConditionForDate for Timestamp filter:

var lowerlimit = DateTime.Today.AddDays(-52).ToString("yyyy-MM-dd");

string dateRangeFilter = TableQuery.CombineFilters(
    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "1005"),
    TableOperators.And,
    TableQuery.GenerateFilterConditionForDate("TimeStamp", QueryComparisons.GreaterThanOrEqual, lowerlimit));

If this is a diagnostics table (e.g. WADLogsTable) I suggest you convert your timestamp to a partition key, then query on that. This would avoid a full table scan since it is an indexed column. Conversion is simple, just prepend "0" to the ticks.

var lowerlimitPartitionKey = DateTimeToPartitionKey(lowerlimit);

// Now query PartitionKey >= lowerlimitPartitionKey
...

where

private string DateTimeToPartitionKey(DateTime dt)
{
    return "0" + dt.Ticks;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!