locked
How do I read the value of a property from Azure table? RRS feed

  • Question

  • Hi,

    This is my first attempt to read some data from an Azure table in my MVC app. I added only one property to my Azure table called UserId which is of Integer type.

    Looks like my code retrieves the record with the proper value. I just haven't been able to grab the value. Here's my code:

    TableResult retrievedResult = table.Execute(retrieveOperation);

    How do I grab the property value from here and put it into an integer variable?


    Thanks, Sam

    Wednesday, April 9, 2014 2:12 AM

Answers

  • Hi

    That's because Azure Table service use EdmType http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.edmtype(v=vs.110).aspx

    You can get data type by codes below:

    private Type GetType(EdmType edmType)
    	{
    		switch (edmType)
    		{
    			case EdmType.Binary : return typeof(byte[]);
    			case EdmType.Boolean : return typeof(bool);
    			case EdmType.DateTime : return typeof(DateTime);
    			case EdmType.Double : return typeof(double);
    			case EdmType.Guid : return typeof(Guid);
    			case EdmType.Int32 : return typeof(int);
    			case EdmType.Int64 : return typeof(long);
    			case EdmType.String : return typeof(string);
    			default: throw new Exception("not supported " + edmType);
    		}
    	}

    Cast the value:

    private object GetValue(EntityProperty property)
    	{
    		switch (property.PropertyType)
    		{
    			case EdmType.Binary : return property.BinaryValue;
    			case EdmType.Boolean : return property.BooleanValue;
    			case EdmType.DateTime : return property.DateTimeOffsetValue;
    			case EdmType.Double : return property.DoubleValue;
    			case EdmType.Guid : return property.GuidValue;
    			case EdmType.Int32 : return property.Int32Value;
    			case EdmType.Int64 : return property.Int64Value;
    			case EdmType.String : return property.StringValue;
    			default: throw new Exception("not supported " + property.PropertyType);
    		}
    	}

    More details please refer to code sample:

    http://code.msdn.microsoft.com/windowsazure/Dynamic-TableServiceEntity-151d661f#content


    My Blog
    Please use Make as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Thursday, April 10, 2014 1:52 AM

All replies

  • hi,

    This method (table.Execute) returns just one entity, rather than a collection, and the returned value in TableResult.Result is a Entity. Specifying both partition and row keys in a query is the fastest way to retrieve a single entity from the Table service. Please see this sample code:

    TableResult retrievedResult = table.Execute(retrieveOperation);
    
    // Print the phone number of the result.
    if (retrievedResult.Result != null)
       Console.WriteLine(((CustomerEntity)retrievedResult.Result).PhoneNumber);
    else
       Console.WriteLine("The phone number could not be retrieved.");

    If you get the data type is string, you could use the 'Convert' method to change the data type.

    Please refer to this sample (http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-table-storage-20/ ).

    Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 9, 2014 6:21 AM
  • i have a sample which showcases how to insert & read azure tables in an asp.net mvc application, please see http://code.msdn.microsoft.com/How-to-add-work-with-cd2f0965

    the most important code snippet here would be:

    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    tableClient.GetTableServiceContext().CreateQuery<TABLE_NAME>("TABLE_NAME").ToList()

    the above code line, will get you a list of all records in your table, strongly typed & so you can simply foreach & access individual properties.

    PS: replace TABLE_NAME with your table name.

    hope this helps!

    Wednesday, April 9, 2014 6:33 AM
  • Will,

    Thanks for your response. I actually got my code from the article that you've referenced.

    I'm able to get the "record" from an Azure table. Where I'm struggling is the "reading the value of the 'UserId' property" which is an integer value.

    I'm new to working with Azure tables so when I inspect the data received in my C# code, I see that the property value comes in as "EntityProperty" and when I look into that, I see different property types like Int32Value, BinaryValue, BooleanValue, etc.

    I just havent' figured out how to grab the value and cast it as an integer. This is where I need help.


    Thanks, Sam

    Wednesday, April 9, 2014 2:07 PM
  • Hi

    That's because Azure Table service use EdmType http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.edmtype(v=vs.110).aspx

    You can get data type by codes below:

    private Type GetType(EdmType edmType)
    	{
    		switch (edmType)
    		{
    			case EdmType.Binary : return typeof(byte[]);
    			case EdmType.Boolean : return typeof(bool);
    			case EdmType.DateTime : return typeof(DateTime);
    			case EdmType.Double : return typeof(double);
    			case EdmType.Guid : return typeof(Guid);
    			case EdmType.Int32 : return typeof(int);
    			case EdmType.Int64 : return typeof(long);
    			case EdmType.String : return typeof(string);
    			default: throw new Exception("not supported " + edmType);
    		}
    	}

    Cast the value:

    private object GetValue(EntityProperty property)
    	{
    		switch (property.PropertyType)
    		{
    			case EdmType.Binary : return property.BinaryValue;
    			case EdmType.Boolean : return property.BooleanValue;
    			case EdmType.DateTime : return property.DateTimeOffsetValue;
    			case EdmType.Double : return property.DoubleValue;
    			case EdmType.Guid : return property.GuidValue;
    			case EdmType.Int32 : return property.Int32Value;
    			case EdmType.Int64 : return property.Int64Value;
    			case EdmType.String : return property.StringValue;
    			default: throw new Exception("not supported " + property.PropertyType);
    		}
    	}

    More details please refer to code sample:

    http://code.msdn.microsoft.com/windowsazure/Dynamic-TableServiceEntity-151d661f#content


    My Blog
    Please use Make as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Thursday, April 10, 2014 1:52 AM