/// <summary> /// Convert a List{T} to a DataTable. /// </summary> public static DataTable ToDataTable<T>(this IList<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) { var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } /// <summary> /// Determine of specified type is nullable /// </summary> public static bool IsNullable(Type type) { return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)); } /// <summary> /// Return underlying type if type is Nullable otherwise return the type /// </summary> public static Type GetCoreType(Type type) { if (type != null && IsNullable(type)) { if (!type.IsValueType) { return type; } else { return Nullable.GetUnderlyingType(type); } } else { return type; } }
Friday, 27 April 2012
List To Datatable
Subscribe to:
Post Comments (Atom)
What should you required to learn machine learning
To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...
-
Introduction This article is aimed at people who are new to the world of Raspberry Pi [Like me]. It gives an idea about what Raspberry P...
-
A simple set of question framings was defined by Kipling in his immortal poem: I have six faithful serving men They taught me all ...
-
In this article we explain how to attain the goal of applying the SAP OpenUI5 user interface library to create line-of-business applicatio...
No comments:
Post a Comment