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

C# 常用工具方法之DataTable(一)

程序员文章站 2023-10-30 23:12:16
1、DataTable 转 泛型T的List 2、DataTable 转 HashTable 3、DataTable 转 HashTable 4、DataTable 按照某列进行条件拆分 日常工作常遇到的utils中积累的代码,可以优化的地方希望多多指教! ......

1、datatable 转 泛型t的list

        /// <summary>
        /// 数据集datatable转换成list集合
        /// </summary>
        /// <typeparam name="t"></typeparam>
        /// <param name="dttemp"></param>
        /// <returns></returns>
        public static list<t> tolistbydatatable<t>(datatable dttemp)
        {
            if (dttemp == null || dttemp.rows.count == 0)
            {
                return null;
            }
            list<t> lstresult = new list<t>();

            for (int j = 0, l = dttemp.rows.count; j < l; j++)
            {
                t _t = (t)activator.createinstance(typeof(t));
                propertyinfo[] propertys = _t.gettype().getproperties();
                foreach (propertyinfo pi in propertys)
                {
                    for (int i = 0, k = dttemp.columns.count; i < k; i++)
                    {
                        // 属性与字段名称一致的进行赋值    
                        if (pi.name.tolower().equals(dttemp.columns[i].columnname.tolower()))
                        {
                            if (dttemp.rows[j][i] != dbnull.value)
                            {
                                switch (pi.propertytype.tostring())
                                {
                                    case "system.int32":
                                        pi.setvalue(_t, nall.toint(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.int64":
                                        pi.setvalue(_t, nall.tolong(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.datetime":
                                        pi.setvalue(_t, nall.todatetime(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.string":
                                        pi.setvalue(_t, dttemp.rows[j][i].tostring(), null);
                                        break;
                                    case "system.boolean":
                                        pi.setvalue(_t, nall.toboolean(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.guid":
                                        pi.setvalue(_t, nall.toguid(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.single":
                                        pi.setvalue(_t, convert.tosingle(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.double":
                                        pi.setvalue(_t, convert.todouble(dttemp.rows[j][i].tostring()), null);
                                        break;
                                    case "system.object":
                                        pi.setvalue(_t, dttemp.rows[j][i], null);
                                        break;
                                }
                            }
                            else
                            {
                                switch (pi.propertytype.tostring())
                                {
                                    case "system.int32":
                                        pi.setvalue(_t, -1, null);
                                        break;
                                    case "system.int64":
                                        pi.setvalue(_t, -1, null);
                                        break;
                                    case "system.datetime":
                                        pi.setvalue(_t, new datetime(0x76c, 1, 1), null);
                                        break;
                                    case "system.boolean":
                                        pi.setvalue(_t, false, null);
                                        break;
                                    case "system.guid":
                                        pi.setvalue(_t, guid.empty, null);
                                        break;
                                    case "system.single":
                                        pi.setvalue(_t, 0.0f, null);
                                        break;
                                    case "system.double":
                                        pi.setvalue(_t, 0.0, null);
                                        break;
                                    case "system.string":
                                        pi.setvalue(_t, string.empty, null);
                                        break;
                                    default:
                                        pi.setvalue(_t, null, null);
                                        break;
                                }
                            }
                            break;
                        }
                    }
                }
                lstresult.add(_t);
            }
            return lstresult;
        }

2、datatable 转 hashtable

        /// <summary>
        /// datatable转hashtable
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static hashtable tohashtablebydatarow(datatable dt, int key)
        {
            hashtable ht = new hashtable();
            for (int i = 0; i < dt.columns.count; i++)
            {
                ht.add(dt.columns[i].columnname, dt.rows[key][i]);
            }
            return ht;
        }

3、datatable 转 hashtable

        /// <summary>
        ///  转换哈希表
        /// </summary>
        /// <typeparam name="t"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static list<hashtable> tohashtablebydatatable<t>(datatable dt)
        {
            list<hashtable> listht = new list<hashtable>();
            for (int i = 0; i < dt.rows.count; i++)
            {
                hashtable ht = new hashtable();
                ht.add(i, dt.rows[i]);
                listht.add(ht);
            }
            return listht;
        }

4、datatable 按照某列进行条件拆分

        /// <summary>
        /// 拆分dt,返回相同数据结构的多个dt
        /// [excel需要去掉第一行标题]
        /// </summary>
        /// <param name="dt">原始数据dt</param>
        /// <param name="condition">第几列数据,按照这一列进行拆分</param>
        /// <param name="orderstr">排序条件,dataview的排序条件</param>
        /// <returns></returns>
        public static list<datatable> splitdtsamestruct(datatable dt,int columnnum,string orderstr) {
            //定义返回对象
            list<datatable> listdt = new list<datatable>();
            //按照条件进行筛选
            list<string> conditionstr = new list<string>();
            foreach (datarow dr in dt.rows)
            {
                //筛选字段的值
                string key = dr["column"+ columnnum].tostring();
                //不存在新增进条件conditionstr中
                if (!isexit(conditionstr,key)) {
                    conditionstr.add(key);
                }
            }
            //根据条件conditionstr集合进行拆分dt
            //1、将dt数据源按照字段排序
            dt.columns.add();
            dataview view = new dataview(dt);
            //正序排列
            view.sort = orderstr;//"column"+ columnnum + " asc";
            dt = view.totable();
            foreach (string str in conditionstr)
            {
                //按照条件进行抽取数据
                datarow[] dr = dt.select("column"+ columnnum + "='"+str+"'");
                //抽出的数据存入临时的tempdt中
                datatable tempdt = new datatable();
                tempdt = dt.clone();//拷贝框架
                for (int i = 0; i < dr.length; i++)
                {
                    tempdt.importrow((datarow)dr[i]);
                }
                //tempdt存入返回对象listdt中
                listdt.add(tempdt);
            }
            return listdt;
        }

日常工作常遇到的utils中积累的代码,可以优化的地方希望多多指教!

上一篇: 醉语

下一篇: 脑残对话两则