using System; using System.Collections.Generic; using System.Data; using System.
Reflection; using System.Text; namespace Core.Util { public static partial class
Extention { /// <summary> /// DataTable turn List /// </summary> /// <typeparam
name="T"> Conversion type </typeparam> /// <param name="dt"> data source </param> ///
<returns></returns> public static List<T> ToList<T>(this DataTable dt) { List<T>
list= new List<T>(); // Confirm that the parameters are valid , Return if invalid Null if (dt == null) return list; else if (
dt.Rows.Count == 0) return list; Dictionary<string, string> dicField = new
Dictionary<string, string>(); Dictionary<string, string> dicProperty = new
Dictionary<string, string>(); Type type = typeof(T); // Create Field Dictionary , Easy to find field names type.
GetFields().ForEach(aFiled => { dicField.Add(aFiled.Name.ToLower(), aFiled.Name)
; }); // Create attribute dictionary , Easy to find property names type.GetProperties().ForEach(aProperty => { dicProperty.
Add(aProperty.Name.ToLower(), aProperty.Name); }); for (int i = 0; i < dt.Rows.
Count; i++) { // Creating generic objects T _t = Activator.CreateInstance<T>(); for (int j = 0; j <
dt.Columns.Count; j++) { string memberKey = dt.Columns[j].ColumnName.ToLower();
// Field assignment if (dicField.ContainsKey(memberKey)) { FieldInfo theField = type.GetField
(dicField[memberKey]); var dbValue = dt.Rows[i][j]; if (dbValue.GetType() ==
typeof(DBNull)) dbValue = null; if (dbValue != null) { Type memberType =
theField.FieldType; dbValue = dbValue.ChangeType_ByConvert(memberType); }
theField.SetValue(_t, dbValue); } // Attribute assignment if (dicProperty.ContainsKey(memberKey))
{ PropertyInfo theProperty = type.GetProperty(dicProperty[memberKey]); var
dbValue= dt.Rows[i][j]; if (dbValue.GetType() == typeof(DBNull)) dbValue = null;
if (dbValue != null) { Type memberType = theProperty.PropertyType; dbValue =
dbValue.ChangeType_ByConvert(memberType); } theProperty.SetValue(_t, dbValue); }
} list.Add(_t); } return list; } /// <summary> /// take DataTable Convert to standard CSV character string ///
</summary> /// <param name="dt"> data sheet </param> /// <returns> Return to standard CSV</returns>
public static string ToCsvStr(this DataTable dt) { // Comma with half width ( Namely ,) As separator , If the column is empty, it is necessary to express its existence .
// If half width comma exists in column content ( Namely ,) Half quotation marks are used ( Namely "") Include the field value .
// If half quotation marks exist in column contents ( Namely ") It should be replaced by half angle double quotation marks ("") Paraphrase , Use half angle quotation marks ( Namely "") Include the field value . StringBuilder sb = new
StringBuilder(); DataColumn colum; foreach (DataRow row in dt.Rows) { for (int i
= 0; i < dt.Columns.Count; i++) { colum = dt.Columns[i]; if (i != 0) sb.Append(
","); if (colum.DataType == typeof(string) && row[colum].ToString().Contains(","
)) { sb.Append("\"" + row[colum].ToString().Replace("\"", "\"\"") + "\""); }
else sb.Append(row[colum].ToString()); } sb.AppendLine(); } return sb.ToString()
; } } }

Technology
©2020 ioDraw All rights reserved
LeetCode quick get start ① —— Array series ( Often asked in an interview , Suggested collection ) Apple iPhone 12 Price leakage : The official highest price is close to 1 Ten thousand yuan ( Essence )2020 year 6 month 26 day C# Class library Ip Address help class spark.sql.shuffle.partitions and spark.default.parallelism The difference between flutter study -- Search box NVIDIA official apology : Robots are sold out RTX 3080django Do not close CSRF middleware , Custom through CSRF Tested post request 【 Deep learning 】 Target detection in progress IOU The concept and calculation of ( Essence )2020 year 6 month 26 day C# Class library DataTable( Extension method ) Can Huawei Hongmeng system and Android , Apples compete against each other ?