title: Serialize C# object into JSON which contains a List
body: <p>What I'm trying to do is to get some data from my database (customers order with all items he ordered) convert it to JSON that contains a list of said items so I can send it with a POST request.
  I want to create a JSON object which would contain details of customers order, and a list of products contained in that order. This is how I want my JSON to look like:</p>
  <pre><code>{
    &quot;account_code&quot;: &quot;00000&quot;,       
    &quot;advance_payment&quot;: false,       
    &quot;date&quot;: [today's date],
    &quot;order_items&quot;: 
     [
       {
         &quot;item_id&quot;: 0, 
         &quot;product_Price &quot;: &quot;105.00&quot;,  
         &quot;order_Item_Rebate: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000001&quot;,   
         &quot;quantity&quot;: 1
       },
       {
         &quot;item_id&quot;: 1, 
         &quot;product_Price &quot;: &quot;55.00&quot;,  
         &quot;price&quot;: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000002&quot;,   
         &quot;quantity&quot;: 5
       },
       {
         &quot;item_id&quot;: 2, 
         &quot;product_Price &quot;: &quot;15.00&quot;,  
         &quot;price&quot;: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000003&quot;,   
         &quot;quantity&quot;: 3
       }
     ]
  }
  </code></pre>
  <p>But what I'm getting is this:</p>
  <pre><code>{
    &quot;account_code&quot;: &quot;00000&quot;,       
    &quot;advance_payment&quot;: false,       
    &quot;date&quot;: [today's date],
    &quot;order_items&quot;: 
     [
       {
         &quot;item_id&quot;: 0, 
         &quot;roduct_Price &quot;: &quot;105.00&quot;,  
         &quot;order_Item_Rebate: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000001&quot;,   
         &quot;quantity&quot;: 1
       }
     ]
  },
  {
    &quot;account_code&quot;: &quot;00000&quot;,       
    &quot;advance_payment&quot;: false,       
    &quot;date&quot;: [today's date],
    &quot;order_items&quot;: 
     [
       {
         &quot;item_id&quot;: 1, 
         &quot;roduct_Price &quot;: &quot;55.00&quot;,  
         &quot;price&quot;: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000002&quot;,   
         &quot;quantity&quot;: 5
       }
     ]
  },
  {
    &quot;account_code&quot;: &quot;00000&quot;,       
    &quot;advance_payment&quot;: false,       
    &quot;date&quot;: [today's date],
    &quot;order_items&quot;: 
     [
       {
         &quot;item_id&quot;: 2, 
         &quot;product_Price &quot;: &quot;15.00&quot;,  
         &quot;price&quot;: &quot;&quot;,     
         &quot;product_code&quot;: &quot;000003&quot;,   
         &quot;quantity&quot;: 3
       }
     ]
  }
  </code></pre>
  <p>Instead of one JSON object with a list of items, I get multiple JSON object for the same customer(account_code).</p>
  <p>I'm getting this data from database using ADO.NET and I read this data with SqlDataReader. Inside SqlDataReader I created a new object and filled it with details about my order. It all works, but problem is order_items which should be a list of all items from the order. I want one order with multiple items in it, not a different order for every item.
  Using code below I am able to get data from my database, but I don't know how to group order_items with the order they should be in.</p>
  <pre><code>List&lt;OrderModel&gt; orderList = new List&lt;OrderModel&gt;();
  using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
                      {
                          while (rdr.Read())
                          {
                              OrderModel order = new OrderModel()
                              {
                                  Account_code = rdr[0].ToString(),
                                  Advance_payment = false,
                                  Date = DateTime.Now.Date,
                                  Order_Items = new List&lt;Order_items&gt;()
                                      {
                                       //Here is where I should do something instead of creating new
                                       //Order_items, but I don't know what
                                          new Order_items()
                                          {
                                              Order_Item_Rebate = rdr[1].ToString(),
                                              Product_Price = rdr[2].ToString(),
                                              Product_Code = rdr[3].ToString(),
                                              Quantity = rdr[4].ToString()
                                          }
                                      }
  
                              };
                              orderList.Add(order);
                          }
                          var jsonString = JsonConvert.SerializeObject(orderList);
                          return jsonString;
                      }
  </code></pre>
  <p>My OrderModel class looks like this:</p>
  <pre><code>public class OrderModel
      {
          public string Account_code { get; set; } 
          public bool Advance_payment { get; set; } 
          public DateTime Date { get; set; }
          public List&lt;Order_items&gt; Order_Items { get; set; } 
      }
  
      public class Order_items
      {
          public string Order_Item_Rebate { get; set; } 
          public string Product_Price { get; set; } 
          public string Product_Code { get; set; }
          public string Quantity { get; set; }
      }
    
  </code></pre>
  <p>And table from my database looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Account_code</th>
  <th>Order_Item_Rebate</th>
  <th>Product_Price</th>
  <th>Product_Code</th>
  <th>Quantity</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>00000</td>
  <td>10</td>
  <td>105</td>
  <td>000001</td>
  <td>1</td>
  </tr>
  <tr>
  <td>00000</td>
  <td>5</td>
  <td>55</td>
  <td>000002</td>
  <td>5</td>
  </tr>
  <tr>
  <td>00000</td>
  <td>15</td>
  <td>15</td>
  <td>000002</td>
  <td>3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have seen similar questions and people were advising to use an array of dictionaries, but I'm not sure if I understand how would they help. I'm new with this, so I'm sorry if question is not really smart.</p>