Wednesday, June 17, 2015

C#, asp.net: Fetch filtered data(string,date,int) from Azure Storage Table using .net

In my previous post I explained how can we fetch data from Azure Storage Table. Now I am going to explain how can we fetch filtered data:
Please visit previous post to start from scratch:


static void Main(string[] args)
{
    int counter = 0;
    StringBuilder sb = new StringBuilder();

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

    // Create the CloudTable object that represents the "TableName1" table.
    CloudTable table = tableClient.GetTableReference("TableName1");

    // Construct the query operation for all customer entities where PartitionKey!="nitin".
    string FilterCounterName = TableQuery.GenerateFilterCondition("ColumnName1", QueryComparisons.Equal, "nitin dhiman");
    string FilterTimeStamp = TableQuery.GenerateFilterConditionForDate("TIMESTAMP", QueryComparisons.GreaterThan, DateTimeOffset.Now.AddDays(-1).Date);

    string FinalFilter = TableQuery.CombineFilters(FilterCounterName, TableOperators.And, FilterTimeStamp);

    TableQuery<TableName1> query = new TableQuery<TableName1>().Where(FinalFilter);
    // Print the fields for each customer.
    foreach (TableName1 entity in table.ExecuteQuery(query))
    {
        if (counter > 500) break;
        counter++;
        Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
        sb.AppendFormat("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
        entity.PartitionKey,
        entity.RowKey,
        entity.CustomCloumn1,
        entity.CustomCloumn2,
        entity.CustomCloumn3,
        entity.CustomCloumnN,
        entity.TIMESTAMP);
    }
    Console.WriteLine(counter);
    Console.WriteLine(sb.ToString());

    Console.WriteLine("Done... press a key to end.");
    Console.ReadKey();
}

Congrats All Done. Run and check. Let me know if you face any issue.



No comments:

Post a Comment