Simone Spagna
Simone Spagna

Reputation: 654

How do I select record from Azure table storage

i want to do a query statement on a Azure table storage. Below the code I wrote :

    public List<T> RetrieveEntity<T>(string Query = null) where T : TableEntity, new()
    {
        try
        {
            // Create the Table Query Object for Azure Table Storage  
            TableQuery<T> DataTableQuery = new TableQuery<T>();
            if (!String.IsNullOrEmpty(Query))
            {
                DataTableQuery = new TableQuery<T>().Where(Query);
            }
           IEnumerable<T> IDataList = table.ExecuteQuery(DataTableQuery); 
            List<T> DataList = new List<T>();
            foreach (var singleData in IDataList)
                DataList.Add(singleData);
            return DataList;
        }
        catch (Exception ExceptionObj)
        {
            throw ExceptionObj;
        }
    }

How do I put in the following row ?

IEnumerable IDataList = table.ExecuteQuery(DataTableQuery);

in order to make all working?

Thanks, Simone

Upvotes: 0

Views: 1165

Answers (1)

tmaj
tmaj

Reputation: 35135

Here's one way to do it.

Step 1. Get the query. Here's an example from Writing LINQ queries against the Table service

var query = from entity in dataServiceContext.CreateQuery<SampleEntity>(tableName)  
                 where entity.PartitionKey == "MyPartitionKey"  
                 select new { entity.RowKey };  

or

var partitionQuery = TableQuery.GenerateFilterCondition
                     ("PartitionKey", QueryComparisons.Equal, myKey);

or

var startQ = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, start);
var endQ= TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, end);
var q = TableQuery.CombineFilters(startQ, TableOperators.And, endQ);

Step 2. Get the data

public async Task<List<T>> GetDataAsync<T>(TableQuery<T> query)
{
    var l = new List<T>();
    TableContinuationToken continuationToken = null;
    do
    {
        var queryResponse = await table.ExecuteQuerySegmentedAsync(query, continuationToken);
        continuationToken = queryResponse.ContinuationToken;
        l.AddRange(queryResponse.Results);
    } 
    while (continuationToken != null);
    return l;
}

Upvotes: 1

Related Questions