首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#中将DataTable转为List方法 . 调用的方法如何写,越全越好. 多谢了

2013-11-15 
c#中将DataTable转为List方法 .调用的方法怎么写,越全越好. 谢谢了 /// summary/// c#中将DataTable转为

c#中将DataTable转为List方法 . 调用的方法怎么写,越全越好. 谢谢了
 /// <summary>
    /// c#中将DataTable转为List方法 .

    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="table"></param>
    /// <returns></returns>
    public List<T> GetList<T>(DataTable table)
    {
        List<T> list = new List<T>();
        T t = default(T);
        PropertyInfo[] propertypes = null;
        string tempName = string.Empty;
        foreach (DataRow row in table.Rows)
        {
            t = Activator.CreateInstance<T>();
            propertypes = t.GetType().GetProperties();
            foreach (PropertyInfo pro in propertypes)
            {
                tempName = pro.Name;
                if (table.Columns.Contains(tempName))
                {
                    object value = row[tempName];
                    if (value.GetType() == typeof(System.DBNull))
                    {
                        value = null;
                    }
                    pro.SetValue(t, value, null);
                }
            }
            list.Add(t);
        }
        return list;
    } 
[解决办法]
list<实体> list = getlist<实体>(dt);

热点排行