ITPub博客

首页 > Linux操作系统 > Linux操作系统 > c# DataTable轉List

c# DataTable轉List

原创 Linux操作系统 作者:CodeAgriculture 时间:2013-04-26 15:27:21 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/28699126/viewspace-759354/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: sqlServer基础
下一篇: Json轉換(一)
请登录后发表评论 登录
全部评论

注册时间:2013-03-07

  • 博文量
    54
  • 访问量
    435952