title: Using Variables to call a table and columns in Linq to Sql
body: <p>I have a problem which seems simple in a logical way but not in a sql way</p>
  
  <p>I have a program which allows the user to select a single radio button, the checkChange event sends the text string of the button to a function that compares it to a database which returns the name of the column heading and the name of the table the column comes from as a keyValuePair. simple so far.</p>
  
  <p>There are 8 tables and each table has between 10 and 21 possible choices and each choice is a separate column in one of the tables.</p>
  
  <p>What I am trying to do is call a table using a string variable as the table name, named in the keyValuePair.value and then select all the data from the column named by the keyValuePair.key.
  if that makes sense.</p>
  
  <p>Edited.</p>
  
  <p>There is a class of static methods which initialises the data context and gets the tables, each method name is composed of "get" + TableName + "Table()", so we have for example getDescriptionsTable(), this class is called Initialise. getTableValues() returns a KeyValuePair.</p>
  
  <pre><code>    KeyValuePair&lt;string, string&gt; names = new getTableValues();
      string colHead = names.Key; // contains the column heading
      string tabName = name.Value; // contains the Table name
  
      string tableName = "get" + tabName + "Table()" // produce a getTable name
  
      MyDataContext mdc = Initialise.tableName; // Initialise is a class for getting tables.
                                                // table name is the concat variable above
      var p = from x in mdc.tableName  // here I want to use the variable tableName
              where !x.colHead.equals( null) &amp;&amp;  // here I want to use the variable colHead
                    !x.colHead.contains("")      //  "   "  "    "  "   "    "         "
  
              select new { Code = x.code,  Property = Convert.ToDecimal(x.colHead) };
      var y = from output in p
              orderby y.Code descending
              select new { Code = y.Code, Property = y.Property};
  
      DataGridView1.DataSource = y;
  
      class Initialise
      {
        #region Full Table
  
        public static System.Data.Linq.Table&lt;Login&gt; GetLoginDetails()
        {
            DataClasses2DataContext log = new DataClasses2DataContext();
            return log.GetTable&lt;Login&gt;();
        }
  
        public static System.Data.Linq.Table&lt;Descriptions&gt; GetDescriptionsTable()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();
            return dc.GetTable&lt;Descriptions&gt;();
        }
  
        public static System.Data.Linq.Table&lt;Singles&gt; GetSingleTable()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();
            return dc.GetTable&lt;Singles&gt;();
        }
  
        public static System.Data.Linq.Table&lt;Doubles&gt; GetDoubleTable()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();
            return dc.GetTable&lt;Doubles&gt;();
        }
        #endregion
      }
  </code></pre>
  
  <p>at this point I have not implemented the join on the second table. That can come later when I understand this problem more fully.</p>
  
  <p>I suppose the SQL equivalent would be
          select @colName from @tableName
          where @colName == NotNull
          and @colName != ""
          order by @colName desc</p>
  
  <p>I am new to Linq to SQL and there may be a simpler way of doing this. But it may not be possible at all. It would mean a lot of if else statements.</p>