title: Getting data from database at runtime with LINQ
body: <p>I want to be able to write and update my database sending an object to my service and then letting the service dynamically decides what kind of object it is to write or update to the database.</p>
  
  <p>This is what I do when doing it statically (example of an update)</p>
  
  <pre><code>switch ((string)property.GetValue(oLinq, null))
                  {
                      case "news":
                          t_news oNews = (t_news)oLinq;                            
                          List&lt;t_news&gt; newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                          t_news oNe = newsList[0];
                          i = 0;
                          foreach (System.Reflection.PropertyInfo p in oNews.GetType().GetProperties().ToList())
                          {
                              if (p.GetValue(oNews, null) != null)
                              {
                                  typeof(t_news).GetProperties().ToList()[i].SetValue(oNe, p.GetValue(oNews, null), null);
                              }
                              i++;
                          }
                          oNe.dLastUpdate = DateTime.Now;
                          oDB.SubmitChanges();
                          oLinq = (object)oNews;
                          break;
  return oLinq;
  </code></pre>
  
  <p>oDB is my DataContext.
  The oLinq is just an object that contains the data I want to update.
  It contains a field in which I specify what kind of table needs to be updated.
  I use a switch case to determine to specify that table.
  I now have 4 different cases in which I do pretty much the same.</p>
  
  <p>But I want to be able to do this dynamically so I don't have to rewrite all this code 4 times, and be able to add new tables in the future.</p>
  
  <p>This is what I'm trying:</p>
  
  <pre><code>List&lt;string&gt; alsTableNames = (from tables in oDB.Mapping.GetTables() select tables.TableName).ToList();
                  foreach (string sTableName in alsTableNames)
                  {
                      if (String.Compare(sTableName.Substring(6), (string)property.GetValue(oLinq, null)) == 0)
                      {
                          string sBasicType = sTableName.Replace("dbo.", "");
                          string sType = "RegisterService." + sBasicType;
                          Type tType = Type.GetType(sType);
                          oLinq = Convert.ChangeType(oLinq, tType);
  </code></pre>
  
  <p>This works to convert the oLinq object to the Type I want.
  But what doesn't work is to get the data out of the database to replace it with the new data.
  I basically need a way to do this in a dynamic way:</p>
  
  <pre><code>List&lt;t_news&gt; newsList = (from n in oDB.t_news where n.ID.Equals(oNews.ID) select n).ToList();
                          t_news oNe = newsList[0];
  </code></pre>
  
  <p>Something like:</p>
  
  <pre><code>List&lt;//type of the table I want&gt; list = (from t in //table where t.//firstproperty.Equals(oLinq.getType().getProperties().toList()[0]) select t.ToList();
                          object oNewObj = list[0];
  </code></pre>
  
  <p>Any Ideas?</p>