title: C# - Adding objects dynamically (adding dynamic property names)
body: <p>I'm trying to create some dynamic <code>ExpandoObject</code>. I've encountered a certain problem. </p>
  
  <p>As I don't know what the name of these different properties in my objects should be, I can't do like this:</p>
  
  <pre><code>var list = new ArrayList();
  
  var obj = new ExpandoObject();
  obj.ID = 1,
  obj.Product = "Pie",
  obj.Days = 1,
  obj.QTY = 65
  
  list.Add(obj);
  </code></pre>
  
  <p>Let me explain my situation: I wish to get data from a random DB (I don't know which, but building a connection string from the information I get from the UI), therefore I don't know what data I need to get. This could be an example of a DB table</p>
  
  <p>TABLE Sale</p>
  
  <ul>
  <li>ID: int,</li>
  <li>Product: nvarchar(100),</li>
  <li>Days: int,</li>
  <li>QTY: bigint</li>
  </ul>
  
  <p>This could be another exmaple:</p>
  
  <p>TABLE Foobar</p>
  
  <ul>
  <li>Id: int,</li>
  <li>Days: int</li>
  <li>QTY: bigint</li>
  <li>Product_Id: int</li>
  <li>Department_Id: int</li>
  </ul>
  
  <p>As you see, I don't know what the DB looks like (this is 100% anonymous, therefore it needs to be 100% dynamic), and the data I want to return should look like a well constructed JSON, like so:</p>
  
  <pre><code>[
    {
      "ID": 1,
      "Product": "Pie"
      "Days": 1,
      "QTY": 65
    },
    {
      "ID": 2,
      "Product": "Melons"
      "Days": 5,
      "QTY": 12
    }
  ]
  </code></pre>
  
  <p>Or, with the other example:</p>
  
  <pre><code>[
    {
      "ID": 1,
      "Days": 2,
      "QTY": 56,
      "Product_Id": 5,
      "Department_Id": 2
    }
    {
      "ID": 2,
      "Days": 6,
      "QTY": 12,
      "Product_Id": 2,
      "Department_Id": 5
    }
  ]
  </code></pre>
  
  <p>I've tried working with these <code>ExpandoObject</code>s, but can't seem to make it work, as I can't do what's illustrated in the top of this question (<em>I don't know the names of the properties</em>). Is there a way for me to say something like:</p>
  
  <pre><code>var obj = new ExpandoObject();
  var propName = "Product";
  
  var obj.propName = "Pie"
  
  Console.WriteLine("Let's print!: " + obj.Product);
  
  //OUTPUT
  Let's print!: Pie
  </code></pre>
  
  <p>Does anyone have a solution, og simply guidance to a structure, that might solve this situation?</p>