Home > Mobile >  How to apply filter in Azure table client
How to apply filter in Azure table client

Time:09-01

I am working on Azure storage table. Below is the partitionKey and rowkey

  1. partitionKey = Product Type
  2. rowKey = Region

I have used below filter to get the data (see the code below). If rowkey (region) is null then it should get product Type from all region. However, It get empty data. If I pass valid Partitionkey(ProductType) and RowKey (region) it's get the data.

AsyncPageable<T> result =TableClient.QueryAsync<T>(filter: $"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'"); 

Note: Partition-key is mandatory

Any advice. Thanks

CodePudding user response:

If rowkey (region) is null then it should get product Type from all region.

If this is what you want, you would need to change the code to something like following:

AsyncPageable<T> result =TableClient.QueryAsync<T>(filter: $"PartitionKey eq '{partitionKey}'"); 

UPDATE

What I would like is If user has provided ProductType and Region then filter using both. In case user only provide ProductType then include all regions.

In this case, you will need to use two different queries - one when both PartitionKey and RowKey are present and other when only PartitionKey is present.

Your code would be something like:

var query = string.IsNullOrWhiteSpace(rowKey) ? 
  $"PartitionKey eq '{partitionKey}'" : $"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'";
AsyncPageable<T> result = TableClient.QueryAsync<T>(filter: query); 

  • Related