Instantly share code, notes, and snippets.

Embed
What would you like to do?
//driver.linq
void Main()
{
var account = "";
var key = "";
var tableName = "";
var cloudTable = GetStorageAccount(account, key).CreateCloudTableClient().GetTableReference(tableName);
var tableQuery = new TableQuery<DynamicTableEntity>().Where(filter: string.Empty);
var timer = Stopwatch.StartNew();
var data = cloudTable.ExecuteQuery(tableQuery).ToList();
data.Dump();
timer.Stop();
timer.Dump();
}
static CloudStorageAccount GetStorageAccount(string accountName, string key, bool useHttps = true)
{
var storageCredentials = new StorageCredentials(accountName, key);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps: useHttps);
return storageAccount;
}
//MyExtensions.linq
void Main()
{
// write code to test your extensions
}
public static class MyExtensions
{
public static void Dump(this object obj)
{
if (obj.GetType() == typeof(List<DynamicTableEntity>))
{
var data = obj as List<DynamicTableEntity>;
if (data == null)
{
obj.Dump();
}
else
{
data.ToDynamicList().Dump("Storage Table Entities");
}
}
else
{
obj.Dump(obj.GetType().Name);
}
}
/// <summary>
/// Goes through <see cref="DynamicTableEntity"/> pickes up each property and its value, converts it to an ExpandoObject
/// </summary>
public static IList<dynamic> ToDynamicList(this IEnumerable<DynamicTableEntity> dynamicTableEntities)
{
var list = new List<dynamic>();
var entityProperties = new HashSet<string>();
//iterate over all rows to collect all properties
foreach (var key in dynamicTableEntities.SelectMany(dynamicTableEntity => dynamicTableEntity.Properties.Keys))
{
entityProperties.Add(key);
}
foreach (var dynamicTableEntity in dynamicTableEntities)
{
var dynamicObject = new ExpandoObject() as IDictionary<string, Object>;
dynamicObject.Add("PartitionKey", (dynamic)dynamicTableEntity.PartitionKey);
dynamicObject.Add("RowKey", (dynamic)dynamicTableEntity.RowKey);
dynamicObject.Add("ETag", (dynamic)dynamicTableEntity.ETag);
dynamicObject.Add("Timestamp", (dynamic)dynamicTableEntity.Timestamp);
foreach (var entityProperty in entityProperties)
{
if (!dynamicTableEntity.Properties.ContainsKey(entityProperty))
{
dynamicObject.Add(entityProperty, (dynamic)string.Empty);
continue;
}
var item = dynamicTableEntity.Properties[entityProperty];
switch (item.PropertyType)
{
case EdmType.Binary:
dynamicObject.Add(entityProperty, (dynamic)item.BinaryValue);
break;
case EdmType.Boolean:
dynamicObject.Add(entityProperty, (dynamic)item.BooleanValue);
break;
case EdmType.DateTime:
dynamicObject.Add(entityProperty, (dynamic)item.DateTimeOffsetValue);
break;
case EdmType.Double:
dynamicObject.Add(entityProperty, (dynamic)item.DoubleValue);
break;
case EdmType.Guid:
dynamicObject.Add(entityProperty, (dynamic)item.GuidValue);
break;
case EdmType.Int32:
dynamicObject.Add(entityProperty, (dynamic)item.Int32Value);
break;
case EdmType.Int64:
dynamicObject.Add(entityProperty, (dynamic)item.Int64Value);
break;
case EdmType.String:
dynamicObject.Add(entityProperty, (dynamic)item.StringValue);
break;
}
}
list.Add(dynamicObject);
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment