Search code examples
c#azureazure-table-storageazure-tablequery

AND operation using TableQuery.CombineFilters on a List


How can I assign initial value to myQuery where I am appending conditions using AND and a foreach loop.

I am trying to do the following:

string myQuery = string.empty;

foreach (string myCondition in myConditionLists)
{
    myQuery = TableQuery.CombineFilters(
        myQuery,
    TableOperators.And,
    TableQuery.GenerateFilterCondition(nameof(MyClass.MyProperty), 
        QueryComparisons.NotEqual, myCondition));
}

When I debug, I see an initial statement of "()" which doesn't seem right. Another approach would be to assign query the first element into myQuery and grow from the second element. Is there an elegant way to do this?


Solution

  • You can specify an integer value and in your loop it adds 1 for each iteration. When it equals 1, you set the initial value to myQuery string.

    The sample code is below:

            static void Main(string[] args)
            {
                CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("your_account", "your_key"),true);
    
                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
                // Create the CloudTable object that represents the "people" table.
                CloudTable table = tableClient.GetTableReference("people");
    
                string myQuery = string.Empty;
    
                List<string> myConditionLists = new List<string>();
                myConditionLists.Add("Ben1");
                myConditionLists.Add("Ben2");
                myConditionLists.Add("Ben3");
                myConditionLists.Add("Ben4");
                myConditionLists.Add("Ben5");
    
                //specify an integer value
                int i = 0;
    
                foreach (string myCondition in myConditionLists)
                {
                    i++;
                    //if i == 1, specify the initial value to the myQuery string.
                    if (i == 1) { myQuery = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.NotEqual, myCondition); }
                    else
                    {
                        myQuery = TableQuery.CombineFilters(
                            myQuery,
                            TableOperators.And,
                            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.NotEqual, myCondition)
                            );
                    }
                }
    
                TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(myQuery);
    
                foreach (CustomerEntity entity in table.ExecuteQuery(query))
                {
                    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                        entity.Email, entity.PhoneNumber);
                }
    
    
                Console.WriteLine("---completed---");
                Console.ReadLine();
            }
    

    My table:

    enter image description here

    The test result:

    enter image description here