c# – 将十进制数据类型存储在Azure表中

前端之家收集整理的这篇文章主要介绍了c# – 将十进制数据类型存储在Azure表中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Windows Azure Table Storage does not support是十进制数据类型.

A suggested workaround是使用自定义属性将十进制属性序列化为字符串:

[EntityDataType(PrimitiveTypeKind.String)]
public decimal Quantity { get; set; }

如何实现此EntityDataType自定义属性,从而可以从Windows Azure Tables存储和检索十进制属性

解决方法

您可以覆盖TableEntity中的WriteEntity方法,并使用EntityResolver
public class CustomTableEntity : TableEntity
{
    private const string DecimalPrefix = "D_";

    public override IDictionary<string,EntityProperty> WriteEntity(OperationContext operationContext)
    {
        var entityProperties = base.WriteEntity(operationContext);
        var objectProperties = GetType().GetProperties();

        foreach (var item in objectProperties.Where(f => f.PropertyType == typeof (decimal)))
        {
            entityProperties.Add(DecimalPrefix + item.Name,new EntityProperty(item.GetValue(this,null).ToString()));
        }

        return entityProperties;
    }
}

我们将使用的实体

public class MyEntity : CustomTableEntity
{
    public string MyProperty { get; set; }

    public decimal MyDecimalProperty1 { get; set; }
    public decimal MyDecimalProperty2 { get; set; }
}

用法包括创建表/插入/检索

#region connection

CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference("mytable");
table.CreateIfNotExists();

#endregion


const string decimalPrefix = "D_";

const string partitionKey = "BlaBlaBla";
string rowKey = DateTime.Now.ToString("yyyyMMddHHmmss");


#region Insert

var entity = new MyEntity
    {
        PartitionKey = partitionKey,RowKey = rowKey,MyProperty = "Test",MyDecimalProperty1 = (decimal) 1.2,MyDecimalProperty2 = (decimal) 3.45
    };

TableOperation insertOperation = TableOperation.Insert(entity);
table.Execute(insertOperation);

#endregion



#region Retrieve

EntityResolver<MyEntity> myEntityResolver = (pk,rk,ts,props,etag) =>
    {
        var resolvedEntity = new MyEntity {PartitionKey = pk,RowKey = rk,Timestamp = ts,ETag = etag};

        foreach (var item in props.Where(p => p.Key.StartsWith(decimalPrefix)))
        {
            string realPropertyName = item.Key.Substring(decimalPrefix.Length);
            System.Reflection.PropertyInfo propertyInfo = resolvedEntity.GetType().GetProperty(realPropertyName);
            propertyInfo.SetValue(resolvedEntity,Convert.ChangeType(item.Value.StringValue,propertyInfo.PropertyType),null);

        }

        resolvedEntity.ReadEntity(props,null);

        return resolvedEntity;
    };

TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey,rowKey,myEntityResolver);
TableResult retrievedResult = table.Execute(retrieveOperation);
var myRetrievedEntity = retrievedResult.Result as MyEntity;

// myRetrievedEntity.Dump(); 

#endregion

猜你在找的C#相关文章