ListToDataTable AND DataTableToListDictionary c#

#region " ListToDataTable " public static DataTable ListToDataTable(this List items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in props) { tb.Columns.Add(prop.Name, prop.PropertyType); } foreach (var item in items) { var values = new object[props.Length]; for (var i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } #endregion #region" DataTableToListDictionary " public static List> DataTableToListDictionary(DataTable dtData) //Datarowu list strige çevirir. { List> lstRows = new List>(); Dictionary dictRow = null; foreach (DataRow dr in dtData.Rows) { dictRow = new Dictionary(); foreach (DataColumn col in dtData.Columns) { dictRow.Add(col.ColumnName, dr[col]); } lstRows.Add(dictRow); } return lstRows; } #endregion

Yorumlar