title: C# How To Access Dynamic Variable Properties in C# and Razor
body: <p>I have the name of a stored procedure stored in a database (and a couple of views). At run time I query the database and then run the needed query. Since I don't know the field names of the query nor do I have a known object to store the results to I use a dynamic variable</p>
  
  <pre><code>dynamic results = Respository.GetTableContents(); 
  // not exactly the code but it conveys the point
  </code></pre>
  
  <p>The table would have an unknown number of fields, but for this example the field names are</p>
  
  <pre><code>Id, FirstName, Lastname  //(this is what the table would be returning) 
  
  // Normally stored to a list of the model type
  List&lt;Users&gt; resultsFromTable = ...
  </code></pre>
  
  <p>The resulting dynamic data can be accessed by</p>
  
  <pre><code>foreach(var r in result)
  {
      string name = r.FirstName + " " + r.LastName;
      //.... Do something with the code, etc.
  }
  </code></pre>
  
  <p>That is great if you know the property names. I don't know the property names.</p>
  
  <p><strong>How do I access the data of the dynamic variable without knowing the property name?</strong></p>
  
  <p><em>My main goal would be to use this in the view (razor).</em> </p>
  
  <p>Maybe I have approached the problem wrong and there is a better way. Any thoughts?</p>