title: C# Assigning fields from unknown list length
body: <p>I'm creating a game in Unity, that allows users to enter SQL commands.</p>
  
  <p>Lets say I have a table called Terminals in my SQL database with the columns Id, Name, Type and Hacked.</p>
  
  <p>If the user enters a SELECT command, e.g <code>SELECT Id, Name FROM Terminals;</code> then the lineList gets added the number of data fields that get returned, e.g. Id and Name only.</p>
  
  <p>My question is, how do I assign the properties in the Terminals class to the correct values that get returned from the SQL query?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class SQLConnect : MonoBehaviour {
  private void Query(string sqlCommand)
  {
      using (dbCon = new SqlConnection(connectionString))
      {
          using (dbcmd = dbCon.CreateCommand())
          {
              dbcmd.CommandText = sqlCommand;
              dbCon.Open();
  
              using (reader = dbcmd.ExecuteReader())
              {
                  var readList = new List&lt;List&lt;object&gt;&gt;();
  
                  while (reader.Read())
                  {
                      var lineList = new List&lt;object&gt;();
  
                      for (int i = 0; i &lt; reader.FieldCount; i++)
                      {
                          lineList.Add(reader.GetValue(i)); // This reads the entries in a row
                      }
  
                      readList.Add(lineList);
                  }
              }
          }
      }
  }
  }
  
  public static class SQLDynamicData
  {
      public static List&lt;Terminals&gt; TerminalList;
  
      public class Terminals
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public string Type { get; set; }
          public bool Hacked { get; set; }
  
          public Terminals(int id, string name, string type, bool hacked)
          {
              Id = id;
              Name = name;
              Type = type;
              Hacked = hacked;
          }
      }
  }
  </code></pre>
  
  <p>Here's what I have tried:</p>
  
  <pre><code>using (reader = dbcmd.ExecuteReader())
              {
                  var readList = new List&lt;List&lt;object&gt;&gt;();
  
                  while (reader.Read())
                  {
                      var lineList = new List&lt;object&gt;();
  
                      for (int i = 0; i &lt; reader.FieldCount; i++)
                      {
                          lineList.Add(reader.GetValue(i)); // This reads the entries in a row
                      }
                   readList.Add(lineList);
                  }
                  foreach (var item in readList)
                  {
                      SQLDynamicData.TerminalList.Add(new SQLDynamicData.Terminals(Convert.ToInt32(item[0]), item[1].ToString(), item[2].ToString(), Convert.ToBoolean(item[3])));
                  }
              }
  </code></pre>
  
  <p>The problem with this is, that obviously if the data returned is just Id and Name, then item[2] and item[3] throw exceptions. Also, Name might be returned first and not Id if the user just selects Name and nothing else, in which case converting item[0] to an int isn't correct</p>
  
  <p>How do I do this? I need to do this because I want to update my in game objects based on SQL data.</p>