受欢迎的博客标签

How to convert BsonBocument to Data table without declare class with mongodb c# driver

Published
public DataTable GetDataTableFromMongoCursor(MongoCursor cursor)
    {
        if (cursor != null && cursor.Count() > 0)
        {

            DataTable dt = new DataTable(cursor.ToString());
            foreach (BsonDocument doc in cursor)
            {

                foreach (BsonElement elm in doc.Elements)
                {
                    if(!dt.Columns.Contains(elm.Name))
                    {
                        dt.Columns.Add(new DataColumn(elm.Name));
                    }

                }
                DataRow dr = dt.NewRow();
                foreach (BsonElement elm in doc.Elements)
                {
                    dr[elm.Name] = elm.Value;

                }
                dt.Rows.Add(dr);
            }
            return dt;

        }
         return null;
    }

http://stackoverflow.com/questions/8612698/how-to-convert-mongodb-bsondocumnet-to-list-of-collection-in-c.