ArrayList<hastable> 转换成 datatable

1-

            try
            {
                DataTable dtResult = new DataTable();
                if (arrayList == null || arrayList.Count < 1)
                {
                    return dtResult;
                }
                if (arrayList.Count > 0)
                {

                    Hashtable ht = (Hashtable)arrayList[0];
                    ArrayList keys = new ArrayList(ht.Keys);
                    foreach (string skey in keys)
                    {
                        Type t = null;    
                        t = ht[skey].ToString().Equals("System.Collections.Hashtable") ? typeof(String) : ht[skey].GetType();
                        dtResult.Columns.Add(new DataColumn(skey, t));

                    }
                    foreach (DataColumn column in dtResult.Columns)
                    {
                        if (column.ColumnName == "POLICY" || column.ColumnName == "BASIS" || column.ColumnName == "FUND")
                        {
                            column.DataType = typeof(Byte[]);
                        }
                    }
                }

                for (int i = 0; i < arrayList.Count; i++)
                {
                    DataRow dr = dtResult.NewRow();
                    Hashtable ht = (Hashtable)arrayList[i];
                    ArrayList keys = new ArrayList(ht.Keys);
                    foreach (string skey in keys)
                    {
                        if (ht[skey] == null || (ht[skey] != null && string.IsNullOrEmpty(ht[skey].ToString())))
                        {
                            dr[skey] = null;
                        }
                        else
                        {

                            dr[skey] = ht[skey].ToString().Equals("System.Collections.Hashtable") ? ((Hashtable)ht[skey])["value"] : ht[skey];

                        }

                    }
                    dtResult.Rows.Add(dr);
                }

                return dtResult;
            }
            catch (System.Exception)
            {

                throw;
            }

  

猜你喜欢

转载自www.cnblogs.com/Isolate/p/11911051.html