//创建一个Dictionary<string, object>对象Dictionary<string,object> dictionary =newDictionary<string,object>();//创建一个DataTable对象,用于存储Dictionary的数据DataTable dataTable =newDataTable();//将Dictionary的键作为列名,值作为数据行的值,添加到DataTable中foreach(KeyValuePair<string,object> kvpindicti...
public static List<T> ConvertDataTableToList<T>(DataTable dataTable) where T : new() { ...
方法一、使用linq List<Dictionary<string, object>> dict = dt.AsEnumerable().Select( row => dt.Columns.Cast<DataColumn>().ToDictionary( column => column.ColumnName, // 键 column => row[column] as object // 值 )).ToList(); 方法二、使用foreach循环 List<Dictionary<string, object>> dict...
property.SetValue(item, value); }else{objectvalue =Convert.ChangeType(row[columnName], property.PropertyType); property.SetValue(item, value); } } }returnitem; }
object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. ...
foreach (object value in pair.Value)dt.Rows.Add(value);} Dictionary转JSON 使用Json.NET序列化:csharp string json = JsonConvert.SerializeObject(dict);JSON转List 解析JSON字符串并转换为List:csharp var list = JsonConvert.DeserializeObject<List>(json);JSON转Dictionary 解析JSON字符串并转换...
return lst; } publicstaticList<T>DataConvert<T>(DataTabletb,refList<T>lst){for(inti=0;i<tb.Rows.Count;i++){lst.Add(DataConvert<T>(tb.Rows[i]));}returnlst;}publicstaticTDataConvert<T>(DataRowrow){vartype=typeof(T);objectobj=type.Assembly.CreateInstance(type.FullName);varc=(T)obj...
{objectvalue=dr[item.Name];if(value.ToString()!=""){item.SetValue(t,value.ToString(),null);}}}list.Add(t);}returnlist;}/// <summary>/// List转DataTable/// </summary>/// <typeparam name="T"></typeparam>/// <param name="list"></param>/// <returns></returns>publicstatic...
public override void Execute(){ using (var conn = DbConnection){ conn.Open();MySqlCommand Command = new MySqlCommand(QuerySql, conn);var data = new DataTable().LoadForReader(Command.ExecuteReader());foreach (var item in data.ToList<Admin>(new List<ConvertMapping> { ConvertMapping.Convert(...
30var values = new object[props.Length];31 32for (int i = 0; i < props.Length; i++)33 { 34 values[i] = props[i].GetValue(item, null);35 } 36 37 tb.Rows.Add(values);38 } 39 40return tb;41 } 42 43///<summary> 44/// Determine of specified type is ...