• C# DataTable 和List之间相互转换的方法


    http://www.cnblogs.com/YoungPop-Chen/p/3259537.html#undefined

     public class TypeConversion
        {
            #region list<T>转datatable
            public static DataTable ToDataTable<T>(List<T> items)
            {
                var tb = new DataTable(typeof(T).Name);
    
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                foreach (PropertyInfo prop in props)
                {
                    Type t = GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
    
                foreach (T item in items)
                {
                    var values = new object[props.Length];
    
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    tb.Rows.Add(values);
                }
    
                return tb;
            }
            public static Type GetCoreType(Type t)
            {
                if (t != null && IsNullable(t))
                {
                    if (!t.IsValueType)
                    {
                        return t;
                    }
                    else
                    {
                        return Nullable.GetUnderlyingType(t);
                    }
                }
                else
                {
                    return t;
                }
            }
            public static bool IsNullable(Type t)
            {
                return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
            }
            #endregion
    
            # region DataTable转换到List
            public static IList<T> ConvertTo<T>(DataTable table)
            {
                if (table == null)
                {
                    return null;
                }
    
                List<DataRow> rows = new List<DataRow>();
    
                foreach (DataRow row in table.Rows)
                {
                    rows.Add(row);
                }
    
                return ConvertTo<T>(rows);
            }
            public static IList<T> ConvertTo<T>(IList<DataRow> rows)
            {
                IList<T> list = null;
    
                if (rows != null)
                {
                    list = new List<T>();
    
                    foreach (DataRow row in rows)
                    {
                        T item = CreateItem<T>(row);
                        list.Add(item);
                    }
                }
    
                return list;
            }
            public static T CreateItem<T>(DataRow row)
            {
                T obj = default(T);
                if (row != null)
                {
                    obj = Activator.CreateInstance<T>();
    
                    foreach (DataColumn column in row.Table.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                        try
                        {
                            object value = row[column.ColumnName];
                            prop.SetValue(obj, value, null);
                        }
                        catch
                        {  //You can log something here     
                           //throw;    
                        }
                    }
                }
    
                return obj;
            }
            #endregion
        }
  • 相关阅读:
    一、初识数据库
    面向对象—基础、名称空间、三大特性
    六、内置函数
    五、迭代器、生成器、装饰器
    四、global和nonlocal、函数名应用、格式化输出
    三、名称空间
    二、函数的参数
    shell脚本
    线性代数
    [模块] python调用java代码-jpype
  • 原文地址:https://www.cnblogs.com/xinbaba/p/7803092.html
Copyright © 2020-2023  润新知