Programming/C# LinqToDataSet Helper brent.lee 2017. 9. 29. 20:15 반응형 // public static class DataSetHelper { private static bool IsNullableType(Type type) { return (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>))); } public static DataTable CreateDataTable<T>(this IEnumerable<T> query, string tableName) { DataTable table = new DataTable(tableName); var fields = typeof(T).GetProperties(); // Create columns foreach (var field in fields) { DataColumn column = new DataColumn(field.Name); column.AllowDBNull = (typeof( T ).IsSubclassOf( typeof( ValueType ) ) ) ? IsNullableType( typeof( T ) ) : true; table.Columns.Add(column); } // Copy rows foreach (var row in query) { object[] values = new object[fields.Length]; for( int i = 0; i < values.Length; i++ ) { values[i] = fields[i].GetValue(row, null); } table.Rows.Add(values); } return table; } } 반응형 저작자표시 비영리 변경금지 (새창열림)