将dataTable转换为 List<T>


 

public class EntityHelper<T> where T : new()

    {

        public static List<T> EntityListGet(DataTable table)

        {

            List<T> entityList = new List<T>();

            if (table != null && table.Rows.Count > 0)

            {

                foreach (DataRow dr in table.Rows)

                {

                    //T model = (T)Activator.CreateInstance(typeof(T));  

                    T entity = new T();

                    for (int i = 0; i < dr.Table.Columns.Count; i++)

                    {

                        PropertyInfo propertyInfo = entity.GetType().GetProperty(dr.Table.Columns[i].ColumnName);

                        if (propertyInfo != null && dr[i] != DBNull.Value)

                        {

                            propertyInfo.SetValue(entity, dr[i], null);

                        }

                    }

                    entityList.Add(entity);

                }

            }

 

            return entityList;

 

        }

    }

 

posted @ 2015-09-10 13:43  SantiagoMunez  阅读(184)  评论(0编辑  收藏  举报