public CustomDataTable ConvertToDataTable(QueryResult queryResult)
        {
            var table = new CustomDataTable();

            if (queryResult.Results == null || !queryResult.Results.Any())
            {
                return(table);
            }

            if (queryResult.IsBsonDocuments)
            {
                return(ConvertBsonDocumentsToDataTable(queryResult.RawResults.Cast <BsonDocument>()));
            }

            foreach (PropertyData prop in queryResult.Properties)
            {
                var column = new DataColumn
                {
                    DataType    = prop.GetUnderLyingType().IsSimpleType() ? prop.GetUnderLyingType() : typeof(CustomDataTable),
                    ColumnName  = prop.Name,
                    AllowDBNull = true
                };
                table.Columns.Add(column);
            }

            foreach (List <object> resultRow in queryResult.Results)
            {
                DataRow dataRow     = table.NewRow();
                int     columnIndex = 0;

                foreach (object obj in resultRow)
                {
                    string propName = queryResult.Properties[columnIndex++].Name;
                    if (obj == null)
                    {
                        dataRow[propName] = DBNull.Value;
                    }
                    else if (table.Columns[propName].DataType == typeof(CustomDataTable))
                    {
                        var placeHolderDataTable = new CustomDataTable {
                            IsLoaded = false, OriginalObject = obj
                        };
                        dataRow[propName] = placeHolderDataTable;
                    }
                    else
                    {
                        dataRow[propName] = obj;
                    }
                }

                table.Rows.Add(dataRow);
            }

            return(table);
        }
        public CustomDataTable ConvertBsonDocumentsToDataTable(IEnumerable <BsonDocument> bsonDocs)
        {
            var table = new CustomDataTable();

            var columns = new List <DataColumn>();

            foreach (var bsonDoc in bsonDocs)
            {
                foreach (var el in bsonDoc.Elements)
                {
                    if (!columns.Exists(x => x.ColumnName == el.Name))
                    {
                        var column = new DataColumn
                        {
                            DataType    = el.Value.IsBsonDocument || el.Value.IsBsonArray ? typeof(CustomDataTable) : typeof(string),
                            ColumnName  = el.Name,
                            AllowDBNull = true
                        };
                        table.Columns.Add(column);
                        columns.Add(column);
                    }
                }
            }

            foreach (var bsonDoc in bsonDocs)
            {
                DataRow dataRow = table.NewRow();

                foreach (var el in bsonDoc.Elements)
                {
                    string propName = el.Name;
                    if (el.Value.IsBsonNull)
                    {
                        dataRow[propName] = DBNull.Value;
                    }
                    else if (el.Value.IsBsonDocument || el.Value.IsBsonArray)
                    {
                        var placeHolderDataTable = new CustomDataTable
                        {
                            IsLoaded       = false,
                            OriginalObject = el.Value.IsBsonDocument ? el.Value.AsBsonDocument : (object)el.Value.AsBsonArray
                        };
                        dataRow[propName] = placeHolderDataTable;
                    }
                    else
                    {
                        dataRow[propName] = el.Value;
                    }
                }

                table.Rows.Add(dataRow);
            }

            return(table);
        }