欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# DataTable转换成实体类

程序员文章站 2022-06-15 09:47:05
...

DataTable转换成实体类

		/// <summary>
        /// DataTable转换成实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        private static List<T> TableToEntity<T>(DataTable dt) where T : class, new()
        {
            //Type type = typeof(T);
            List<T> list = new List<T>();
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    T entity = new T();
                    PropertyInfo[] pArray = entity.GetType().GetProperties();

                    foreach (PropertyInfo p in pArray)
                    {
                        if (dt.Columns.Contains(p.Name))
                        {
                            if (!p.CanWrite) continue;
                            var value = row[p.Name];
                            if (value != DBNull.Value)
                            {
                                Type targetType = p.PropertyType;
                                Type convertType = targetType;
                                if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                                {
                                    //可空类型
                                    NullableConverter nullableConverter = new NullableConverter(targetType);
                                    convertType = nullableConverter.UnderlyingType;
                                }
                                if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString()))
                                {
                                    value = Convert.ChangeType(value, convertType);
                                }
                                switch (convertType.FullName)
                                {
                                    case "System.Decimal":
                                        p.SetValue(entity, Convert.ToDecimal(value), null);
                                        break;
                                    case "System.String":
                                        p.SetValue(entity, Convert.ToString(value), null);
                                        break;
                                    case "System.Int32":
                                        p.SetValue(entity, Convert.ToInt32(value), null);
                                        break;
                                    case "System.Int64":
                                        p.SetValue(entity, Convert.ToInt64(value), null);
                                        break;
                                    case "System.Int16":
                                        p.SetValue(entity, Convert.ToInt16(value), null);
                                        break;
                                    case "System.Double":
                                        p.SetValue(entity, Convert.ToDouble(value), null);
                                        break;
                                    case "System.Single":
                                        p.SetValue(entity, value.ToFloat(), null);
                                        break;
                                    case "System.DateTime":
                                        p.SetValue(entity, Convert.ToDateTime(value), null);
                                        break;
                                    default:
                                        p.SetValue(entity, value, null);
                                        break;
                                }
                            }
                        }
                    }
                    list.Add(entity);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Table转换实体类失败:" + ex.Message);
            }
            return list;
        }
相关标签: C# c#