DataTable轉成List集合

解一:ide

public class ModelConvertHelper<T> where T : new()  // 此處必定要加上new()
    {

        public static IList<T> ConvertToModel(DataTable dt)
        {

            IList<T> ts = new List<T>();// 定義集合
            Type type = typeof(T); // 得到此模型的類型
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T(); 
                PropertyInfo[] propertys = t.GetType().GetProperties();// 得到此模型的公共屬性
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }
View Code


解二:spa

/// <summary>
        /// DataTable 轉換爲List 集合
        /// </summary>
        /// <typeparam name="TResult">類型</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public static List<TResult> ToList<TResult>(DataTable dt) where TResult : class, new()
        {
            //建立一個屬性的列表
            List<PropertyInfo> prlist = new List<PropertyInfo>();
            //獲取TResult的類型實例  反射的入口
            Type t = typeof(TResult);
            //得到TResult 的全部的Public 屬性 並找出TResult屬性和DataTable的列名稱相同的屬性(PropertyInfo) 並加入到屬性列表 
            Array.ForEach<PropertyInfo>(t.GetProperties(), p =>
            {
                if (dt.Columns.IndexOf(p.Name) != -1)
                    prlist.Add(p);
            });
            //建立返回的集合
            List<TResult> oblist = new List<TResult>();

            foreach (DataRow row in dt.Rows)
            {
                //建立TResult的實例
                TResult ob = new TResult();
                //找到對應的數據  並賦值
                prlist.ForEach(p =>
                {
                    if (row[p.Name] != DBNull.Value)
                    {
                        
                        Type tt = row[p.Name].GetType();
                        date = tt.Name;
                    }

                    if (date == "DateTime")
                    {
                    //    if (p.Name == "WorkingDate" || p.Name == "CreateDate" || p.Name == "RptDate" || p.Name == "UpdateDate")
                    //    {
                    //        p.SetValue(ob, row[p.Name].ToString(), null);
                    //    }
                    //    else
                    //    {
                            try
                            {
                                p.SetValue(ob, Convert.ToDateTime(row[p.Name]), null);
                            }
                            catch
                            {

                            }
                        //}
                        
                    }
                    else if (date == "Int32")
                    {
                        //if (p.Name == "IsErr" || p.Name == "State" || p.Name == "SourceType")
                        //{
                        //    p.SetValue(ob, row[p.Name].ToString(), null);
                        //}
                        //else
                        //{
                            try
                            {
                                p.SetValue(ob, Convert.ToInt32(row[p.Name]), null);
                            }
                            catch
                            {

                            }
                        //}

                    }
                    else if (date == "Decimal")
                    {
                    //    if (p.Name == "IsErr")
                    //    {
                        try
                        {
                            p.SetValue(ob, Convert.ToDecimal(row[p.Name]), null);
                        }
                        catch
                        {

                        }
                    //    }
                    //    else
                    //    {
                            //p.SetValue(ob, row[p.Name].ToString(), null);
                        //}
                    }
                    else
                    {
                        try
                        {
                            p.SetValue(ob, row[p.Name], null);
                        }
                        catch
                        {

                        }

                    }
                });
                //放入到返回的集合中.
                oblist.Add(ob);
            }
            return oblist;
        }
View Code

解二親測,解一待測!code

相關文章
相關標籤/搜索