title: How do I get the first element of a dynamic/generic type
body: <p>Is there any way to pass in a generic entity object and get the first in its query.</p>
  
  <p>My intention is to call <code>FirstOrDefault()</code> on every table, and try catch for errors in the databases integrity.</p>
  
  <p>It would have been nicer to be able to call it by string parameter e.g. <code>db.Entity("myTable").FirstOrDefault()</code> but such a method doesn't seem to exist.</p>
  
  <p>Below is some generic <code>T</code> attempt, but i get the object not set to instance error.</p>
  
  <pre><code>   // Called with
     //GetFirstObject&lt;myTable&gt;(db.myTable);
  
      public static void GetFirstObject&lt;T&gt;(object obj)
      {
          MethodInfo method = typeof(IEnumerable&lt;T&gt;).GetMethod("FirstOrDefault");
          MethodInfo generic = method.MakeGenericMethod();
          generic.Invoke(obj, null);            
      }
  </code></pre>