list转DataTable

来源:互联网 发布:数值概率算法 编辑:程序博客网 时间:2024/06/08 17:51
public DataTable ListToDataTable(List<KttranLog> _list)        {            DataTable dt = new DataTable();            if (_list != null)            {                //通过反射获取list中的字段                 System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();                foreach (System.Reflection.PropertyInfo pi in p)                {                    dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));                }                for (int i = 0; i < _list.Count; i++)                {                    IList TempList = new ArrayList();                    //将IList中的一条记录写入ArrayList                    foreach (System.Reflection.PropertyInfo pi in p)                    {                        object oo = pi.GetValue(_list[i], null);                        TempList.Add(oo);                    }                    object[] itm = new object[p.Length];                    for (int j = 0; j < TempList.Count; j++)                    {                        itm.SetValue(TempList[j], j);                    }                    dt.LoadDataRow(itm, true);                }            }            return dt;        }


0 0
原创粉丝点击