Search code examples
azureazure-table-storageazure-tablequery

How to retrieve all entities in Azure Table DB?


The code below retrieves all entities where PartitionKey = "Smith".

If I want to eliminate this constraint and return all entities in the table, how should I modify the below code?

Sorry if it seems a silly question, but I am new to C# and AzureDBs, and the tutorials and sample codes are very limited on official site.

TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));

foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
        entity.Email, entity.PhoneNumber);
}

Solution

  • You could not use the where attribute which is filtering what you want.

    Refer to the following code:TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();

    Here is the completed code:

    static void Main(string[] args)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
        CloudTable table = tableClient.GetTableReference("people");
        table.CreateIfNotExists();
    
        TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>();
        foreach (CustomerEntity entity in table.ExecuteQuery(query))
        {
            Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
            entity.Email, entity.PhoneNumber);
        }
    }
    

    The screen shot: enter image description here

    However please keep in mind that table service returns a maximum of 1000 entities in a single call to it. If there're more than 1000 entities available in your table, it returns a continuation token which can be used to fetch next set of entities. The ExecuteQuery method actually handles this continuation token internally thus if you want to cancel this operation for any reason, you can't do that.

    You could use var queryResult = table.ExecuteQuerySegmented(new TableQuery<MyEntity>(), token); as Gaurav said.

    For more detail, you could refer to this reply.