Esempio n. 1
0
        public static List <T> ToList <T>(this DataTable dataTable)
        {
            var columnNameList = dataTable.Columns.Cast <DataColumn>().Select(c => c.ColumnName).ToList();
            var properties     = typeof(T).GetProperties();

            return(dataTable.AsEnumerable().Select(row =>
            {
                var targetObject = Activator.CreateInstance <T>();
                foreach (var propety in properties)
                {
                    if (columnNameList.Contains(propety.Name))
                    {
                        var propertyInfo = targetObject.GetType().GetProperty(propety.Name);
                        propety.SetValue(targetObject, row[propety.Name] == DBNull.Value ? null : BaseDatabase.ChangeType(row[propety.Name], propertyInfo.PropertyType));
                    }
                }
                return targetObject;
            }).ToList());
        }