簡體   English   中英

Azure 表:如何編寫范圍查詢以過濾分區鍵

[英]Azure Table: How to write a range query to filter on partition key

我將當前 UTC 日期四舍五入到最接近的分鍾並將其轉換為刻度以將結果保存為 Azure 表存儲中的PartitionKey 我想運行一個范圍查詢來獲取代碼中的特定分區鍵范圍。 我正在使用 C#,但顯然不支持以下 LINQ 操作。

var tableQuery = cloudTable.CreateQuery<Log>().Where(x => long.Parse(x.PartitionKey) <= ticks); 

投擲

System.NotSupportedException: 'The expression (Parse([10007].PartitionKey) <= 637224147600000000) is not supported.'

我也嘗試過String.Compare() ,但它也不起作用。 那么,如何在 C# 中編寫過濾查詢以獲取小於或等於給定刻度的記錄? 請注意,以這種方式選擇分區鍵,以便記錄可以自然地按時間降序排序。

下面是我用來獲取數據的示例。 根據您的情況嘗試以下查詢。 我認為它應該工作。

public async Task<Advertisement> GetAdvertisementBy(string id, string user)
    {
        List<Advertisement> list = new List<Advertisement>();
        // Initialize the continuation token to null to start from the beginning of the table.
        TableContinuationToken continuationToken = null;
        var filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, user);
        var filter3 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id);
        var combine = TableQuery.CombineFilters(filter1, TableOperators.And, filter3);
        TableQuery<Advertisement> query = new TableQuery<Advertisement>().Where(combine);
        do
        {
            // Retrieve a segment (up to 1,000 entities).
            TableQuerySegment<Advertisement> tableQueryResult = await base.Table.ExecuteQuerySegmentedAsync(query, continuationToken);
            // Assign the new continuation token to tell the service where to
            // continue on the next iteration (or null if it has reached the end).
            continuationToken = tableQueryResult.ContinuationToken;
            list.AddRange(tableQueryResult.Results);
            // Loop until a null continuation token is received, indicating the end of the table.
        } while (continuationToken != null);

        return list.FirstOrDefault();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM