Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyClass
- {
- public uint ID { get; set; }
- public List<double> Values { get; set; }
- }
- List<MyClass> objects = new List<MyClass>();
- var query = objects.Select(o => new { o.ID, o.Values } ).ToList(); // this example returns only ID field
- public DataTable ConvertToDataTable<T>(IList<T> data)
- {
- DataTable table = new DataTable();
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
- 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;
- }
Add Comment
Please, Sign In to add comment