title: BsonDocument contains an array. Need to access the array elements in C# when the BsonDocument is contained by a C# list
body: <p>I have a mongodb collection "users" which stores documents in the following format -</p>
  
  <pre><code>{
      "_id" : ObjectId("53fe7ae0ef038fee879263d5"),
      "username" : "John"
      "status" : "online",
      "profile" : [ 
          {
              "name" : "John Stuart",
              "age" : "23",
              "gender" : "male"
          }
      ]
  }
  </code></pre>
  
  <p>I use the following function in C#.NET to store documents from the collection into a list of BsonDocuments -</p>
  
  <pre><code>    public static List&lt;BsonDocument&gt; LoadDataByWhere (string table, string whereClause)
      {
          // Here table is the collection name and whereClause is the mongodb query 
  
          var collection = db.GetCollection (table);
          QueryDocument whereDoc = new QueryDocument(BsonDocument.Parse(whereClause));
          var resultSet = collection.Find (whereDoc);
          List&lt;BsonDocument&gt; docs = resultSet.ToList();
  
          if (resultSet.Count() &gt; 0) {
              foreach(BsonDocument doc in docs)
              {
                  doc.Set("_id", doc.GetElement("_id").ToString().Split('=')[1]);
              }
              return docs;
          } 
          else {
              return null;
          }
      }
  </code></pre>
  
  <p>Supposing that I store the List returned in list. I can use -</p>
  
  <pre><code>    List&lt;string&gt; username = new List&lt;string&gt;();
  
      foreach(BsonDocument item in list) 
      {
          username.Add(Convert.ToString(list.getElement("username").Value));
      }
  </code></pre>
  
  <p>But how do I get the array element values like name, age and gender in C# using methods similar to the one described above?</p>