How do you convert a DataTable into a generic list?

前端 未结 27 2890
后悔当初
后悔当初 2020-11-22 17:04

Currently, I\'m using:

DataTable dt = CreateDataTableInSomeWay();

List list = new List(); 
foreach (DataRow dr in dt.Rows)
{
          


        
27条回答
  •  执笔经年
    2020-11-22 17:33

    We can use a Generic Method for converting DataTable to List instead of manually converting a DataTable to List.

    Note: DataTable's ColumnName and Type's PropertyName should be same.

    Call the below Method:

    long result = Utilities.ConvertTo(dt ,out listStudent);
    
    // Generic Method
    public class Utilities
    {
        public static long ConvertTo(DataTable table, out List entity)
        {
            long returnCode = -1;
            entity = null;
    
            if (table == null)
            {
                return -1;
            }
    
            try
            {
                entity = ConvertTo(table.Rows);
                returnCode = 0;
            }
    
            catch (Exception ex)
            {
                returnCode = 1000;
            }
    
            return returnCode;
        }
    
        static List ConvertTo(DataRowCollection rows)
        {
            List list = null;
            if (rows != null)
            {
                list = new List();
    
                foreach (DataRow row in rows)
                {
                    T item = CreateItem(row);
                    list.Add(item);
                }
            }
    
            return list;
        }
    
        static T CreateItem(DataRow row)
        {
            string str = string.Empty;
            string strObj = string.Empty;
    
            T obj = default(T);
    
            if (row != null)
            {
                obj = Activator.CreateInstance();
                strObj = obj.ToString();
                NameValueCollection objDictionary = new NameValueCollection();
    
                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
    
                    if (prop != null)
                    {
                        str = column.ColumnName;
    
                        try
                        {
                            objDictionary.Add(str, row[str].ToString());
                            object value = row[column.ColumnName];
                            Type vType = obj.GetType();
    
                            if (value == DBNull.Value)
                            {
                                if (vType == typeof(int) || vType == typeof(Int16)
                                                         || vType == typeof(Int32)
                                                         || vType == typeof(Int64)
                                                         || vType == typeof(decimal)
                                                         || vType == typeof(float)
                                                         || vType == typeof(double))
                                {
                                    value = 0;
                                }
    
                                else if (vType == typeof(bool))
                                {
                                    value = false;
                                }
    
                                else if (vType == typeof(DateTime))
                                {
                                    value = DateTime.MaxValue;
                                }
    
                                else
                                {
                                    value = null;
                                }
    
                                prop.SetValue(obj, value, null);
                            }
    
                            else
                            {
                                prop.SetValue(obj, value, null);
                            }
                        }
    
                        catch(Exception ex)
                        {
    
                        }
                    }
                }
    
                PropertyInfo ActionProp = obj.GetType().GetProperty("ActionTemplateValue");
    
                if (ActionProp != null)
                {
                    object ActionValue = objDictionary;
                    ActionProp.SetValue(obj, ActionValue, null);
                }
            }
    
            return obj;
        }
    }
    

提交回复
热议问题