title: cannot be able to use "as" key word in DynamicTableEntity (Azure Table)
body: <p>I have an Azure table where I have inserted heterogeneous entities. After the retrieval, I want to convert them to some specific type using "as". I tried to do this, but it threw the following error:</p>
  
  <blockquote>
    <p>Cannot be able to convert DynamicTableEntity to TestingEntity Via reference conversion, boxing conversion, unboxing conversion,  wrapping conversion or null type conversion.</p>
  </blockquote>
  
  <p>Is there any way I can convert my entities to a particular type?</p>
  
  <p>My code is as follows:</p>
  
  <pre><code> CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                  // Create the table client.
                  CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
  
                  CloudTable table = tableClient.GetTableReference("TestingWithTableDatetime");
                  // Create the table if it doesn't exist.
                  table.CreateIfNotExists();
       TableQuery&lt;DynamicTableEntity&gt; entityQuery =
                      new TableQuery&lt;DynamicTableEntity&gt;();
                  var employees = table.ExecuteQuery(entityQuery);
  
                  IEnumerable&lt;DynamicTableEntity&gt; entities = table.ExecuteQuery(entityQuery);
                  foreach (var e in entities)
                  {
                      EntityProperty entityTypeProperty;
                      if (e.Properties.TryGetValue("EntityType", out entityTypeProperty))
                      {
                          if (entityTypeProperty.StringValue == "SampleEntity1")
                          {
  //Cannot be able to Use as
                              var TestingWithTableDatetime = e as SampleEntity1;
                          }
                          if (entityTypeProperty.StringValue == "SampleEntity2")
                          {
                              // Use entityTypeProperty, RowKey, PartitionKey, Etag, and Timestamp
                          }
                          if (entityTypeProperty.StringValue == "SampleEntity3")
                          {
                              // Use entityTypeProperty, RowKey, PartitionKey, Etag, and Timestamp
                          }
                      }
                  }
  </code></pre>
  
  <p>Class definition for Sample1</p>
  
  <pre><code> public class Sample1 : TableEntity
      {
          public Sample1(string pk, string rk)
          {
              this.PartitionKey = pk;
              this.RowKey = rk;
              EntityType = "MonitoringResources";
          }
  
          public string EntityType { get; set; }
  
          public Sample1()
          {
          }
  
  
      }
  </code></pre>
  
  <p>Things I have tried.I have created a class as Testing and in that I inherited Table entity.Then Testing is inherited by sample1 as follow</p>
  
  <p>Testing Class definition</p>
  
  <pre><code>public class testing : TableEntity
      {
          public testing(string pk, string rk)
          {
              this.PartitionKey = pk;
              this.RowKey = rk; //MetricKey
          }
  
          public string EntityType { get; set; }
  
          public testing()
          {
          }
      }
  </code></pre>
  
  <p>modified Class sample1:</p>
  
  <pre><code>public class sample1 : testing
      {
          public sample1(string pk, string rk) : base(pk, rk)
          {
              EntityType = "sample1";
          }
  
          public sample1()
          {
          }
      }
  </code></pre>
  
  <p>In this i didnt get any error but 
  when I am converting it to sample1 by using "as" it returns as null.</p>
  
  <p>Finally I ended with creating some helper.</p>
  
  <pre><code> public static class AzureManager
      {
          /// &lt;summary&gt;
          ///  Converts a dynamic table entity to .NET Object
          /// &lt;/summary&gt;
          /// &lt;typeparam name="TOutput"&gt;Desired Object Type&lt;/typeparam&gt;
          /// &lt;param name="entity"&gt;Dynamic table Entity&lt;/param&gt;
          /// &lt;returns&gt;Output Object&lt;/returns&gt;
          public static TOutput ConvertTo&lt;TOutput&gt;(DynamicTableEntity entity)
          {
              return ConvertTo&lt;TOutput&gt;(entity.Properties, entity.PartitionKey, entity.RowKey);
          }
  
          /// &lt;summary&gt;
          ///  Convert a Dynamic Table Entity to A POCO .NET Object.
          /// &lt;/summary&gt;
          /// &lt;typeparam name="TOutput"&gt;Desired Object Types&lt;/typeparam&gt;
          /// &lt;param name="properties"&gt;Dictionary of Table Entity&lt;/param&gt;
          /// &lt;returns&gt;.NET object&lt;/returns&gt;
          public static TOutput ConvertTo&lt;TOutput&gt;(IDictionary&lt;string, EntityProperty&gt; properties, string partitionKey, string rowKey)
          {
              var jobject = new JObject();
              properties.Add("PartitionKey", new EntityProperty(partitionKey));
              properties.Add("RowKey", new EntityProperty(rowKey));
              foreach (var property in properties)
              {
                  WriteToJObject(jobject, property);
              }
              return jobject.ToObject&lt;TOutput&gt;();
          }
  
          public static void WriteToJObject(JObject jObject, KeyValuePair&lt;string, EntityProperty&gt; property)
          {
              switch (property.Value.PropertyType)
              {
                  case EdmType.Binary:
                      jObject.Add(property.Key, new JValue(property.Value.BinaryValue));
                      return;
  
                  case EdmType.Boolean:
                      jObject.Add(property.Key, new JValue(property.Value.BooleanValue));
                      return;
  
                  case EdmType.DateTime:
                      jObject.Add(property.Key, new JValue(property.Value.DateTime));
                      return;
  
                  case EdmType.Double:
                      jObject.Add(property.Key, new JValue(property.Value.DoubleValue));
                      return;
  
                  case EdmType.Guid:
                      jObject.Add(property.Key, new JValue(property.Value.GuidValue));
                      return;
  
                  case EdmType.Int32:
                      jObject.Add(property.Key, new JValue(property.Value.Int32Value));
                      return;
  
                  case EdmType.Int64:
                      jObject.Add(property.Key, new JValue(property.Value.Int64Value));
                      return;
  
                  case EdmType.String:
                      jObject.Add(property.Key, new JValue(property.Value.StringValue));
                      return;
  
                  default:
                      return;
              }
          }
      }
  </code></pre>
  
  <p>the above one works for me.</p>
  
  <pre><code>var obj= AzureManager.ConvertTo&lt;Sample1&gt;(e);
  </code></pre>
  
  <p>If you find any other way.Please suggest.</p>