C# 完整的DataTable和list转换
1 public class CollectionHelper 2 { 3 private CollectionHelper() 4 { 5 } 6 7 public static DataTable ConvertTo<T>(IList<T> list) 8 { 9 DataTable table = CreateTable<T>(); 10 Type entityType = typeof(T); 11 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); 12 13 foreach (T item in list) 14 { 15 DataRow row = table.NewRow(); 16 17 foreach (PropertyDescriptor prop in properties) 18 { 19 row[prop.Name] = prop.GetValue(item); 20 } 21 22 table.Rows.Add(row); 23 } 24 25 return table; 26 } 27 28 public static IList<T> ConvertTo<T>(IList<DataRow> rows) 29 { 30 IList<T> list = null; 31 32 if (rows != null) 33 { 34 list = new List<T>(); 35 36 foreach (DataRow row in rows) 37 { 38 T item = CreateItem<T>(row); 39 list.Add(item); 40 } 41 } 42 43 return list; 44 } 45 46 public static IList<T> ConvertTo<T>(DataTable table) 47 { 48 if (table == null) 49 { 50 return null; 51 } 52 53 List<DataRow> rows = new List<DataRow>(); 54 55 foreach (DataRow row in table.Rows) 56 { 57 rows.Add(row); 58 } 59 60 return ConvertTo<T>(rows); 61 } 62 63 public static T CreateItem<T>(DataRow row) 64 { 65 T obj = default(T); 66 if (row != null) 67 { 68 obj = Activator.CreateInstance<T>(); 69 70 foreach (DataColumn column in row.Table.Columns) 71 { 72 PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName); 73 try 74 { 75 object value = row[column.ColumnName]; 76 prop.SetValue(obj, value, null); 77 } 78 catch 79 { 80 // You can log something here 81 throw; 82 } 83 } 84 } 85 86 return obj; 87 } 88 89 public static DataTable CreateTable<T>() 90 { 91 Type entityType = typeof(T); 92 DataTable table = new DataTable(entityType.Name); 93 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType); 94 95 foreach (PropertyDescriptor prop in properties) 96 { 97 table.Columns.Add(prop.Name, prop.PropertyType); 98 } 99 100 return table; 101 } 102 } 103 104 105 To see the full code in action, check this sample out: 106 107 public class MyClass 108 { 109 public static void Main() 110 { 111 List<Customer> customers = new List<Customer>(); 112 113 for (int i = 0; i < 10; i++) 114 { 115 Customer c = new Customer(); 116 c.Id = i; 117 c.Name = "Customer " + i.ToString(); 118 119 customers.Add(c); 120 } 121 122 DataTable table = CollectionHelper.ConvertTo<Customer>(customers); 123 124 foreach (DataRow row in table.Rows) 125 { 126 Console.WriteLine("Customer"); 127 Console.WriteLine("---------------"); 128 129 foreach (DataColumn column in table.Columns) 130 { 131 object value = row[column.ColumnName]; 132 Console.WriteLine("{0}: {1}", column.ColumnName, value); 133 } 134 135 Console.WriteLine(); 136 } 137 138 RL(); 139 } 140 141 #region Helper methods 142 143 private static void WL(object text, params object[] args) 144 { 145 Console.WriteLine(text.ToString(), args); 146 } 147 148 private static void RL() 149 { 150 Console.ReadLine(); 151 } 152 153 private static void Break() 154 { 155 System.Diagnostics.Debugger.Break(); 156 } 157 158 #endregion 159 }