May be useful for U: // 1. Query in mongodb
public DataTable get_data_chude()
{
List<ChuDe> lst = new List<ChuDe>();
MongoDatabase mdMongoDatabase = ConnectToMongDB();
MongoCollection<ChuDe> mcMongoCollection = mdMongoDatabase.GetCollection<ChuDe>(strTableChude);
foreach (ChuDe objChuDe in mcMongoCollection.FindAll())
{
lst.Add(objChuDe);
}
return ConvertToDataTable(lst);
}
//2. convet list ra datatable
public DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
Goodluck!
Vào 18:54:52 UTC+7 Thứ tư, ngày 13 tháng hai năm 2013, Guillaume FRANCHET đã viết: