c# DataTable轉List

CodeAgriculture發表於2013-04-26
public class ToModel where T : new()
    {
        ///
        /// 將DataSet轉化為List
        ///

        ///
        ///
        public static List DataSetToList(DataSet ds)
        {
            // 定義集合
            List ts = new List();
            try
            {
                // 獲得此模型的型別
                Type type = typeof(T);
                string tempName = "";
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
                        // 獲得此模型的公用變數
                        PropertyInfo[] propertys = t.GetType().GetProperties();

                        foreach (PropertyInfo proper in propertys)
                        {
                            tempName = proper.Name.ToUpper();

                            // 檢查DataTable是否包含此列
                            if (dt.Columns.Contains(tempName))
                            {
                                object value = dr[tempName];
                                if (value != DBNull.Value)
                                {
                                    if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                        proper.SetValue(t, value, null);
                                    else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                        proper.SetValue(t, value, null);
                                    else
                                    {
                                        if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                        {
                                            string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                            propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                            proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                        }
                                        else
                                            proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                    }
                                }
                            }
                        }
                        ts.Add(t);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ts;
        }

        public static List DataTableToList(DataTable dt)
        {
            // 定義集合
            List ts = new List();
            try
            {
                // 獲得此模型的型別
                    Type type = typeof(T);
                    string tempName = "";
                    foreach (DataRow dr in dt.Rows)
                    {
                        T t = new T();
                        // 獲得此模型的公用變數
                        PropertyInfo[] propertys = t.GetType().GetProperties();

                        foreach (PropertyInfo proper in propertys)
                        {
                            tempName = proper.Name.ToUpper();

                            // 檢查DataTable是否包含此列
                            if (dt.Columns.Contains(tempName))
                            {
                                object value = dr[tempName];
                                if (value != DBNull.Value)
                                {
                                    if (proper.PropertyType == typeof(bool) && value.ToString() == "1")
                                        proper.SetValue(t, value, null);
                                    else if (proper.PropertyType == typeof(bool) && value.ToString() != "1")
                                        proper.SetValue(t, value, null);
                                    else
                                    {
                                        if (proper.PropertyType.ToString().Contains("System.Nullable"))
                                        {
                                            string propertytype = proper.PropertyType.ToString().Substring(proper.PropertyType.ToString().IndexOf('[') + 1);
                                            propertytype = propertytype.Remove(propertytype.LastIndexOf(']'));
                                            proper.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertytype)), null);
                                        }
                                        else
                                            proper.SetValue(t, Convert.ChangeType(value, proper.PropertyType), null);
                                    }
                                }
                            }
                        }
                        ts.Add(t);
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ts;
        }

       
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-759354/,如需轉載,請註明出處,否則將追究法律責任。

相關文章