Esempio n. 1
0
        /// <summary>
        /// List转DataTable
        /// </summary>
        /// <param name="InputList">输入的对象列表</param>
        public static DataTable List2DataTable(List <T> InputList, string ColumnDisplayOrder = "")
        {
            DataTable           reVal                  = new DataTable();
            Type                type                   = typeof(T);
            List <PropertyInfo> PropertyArr            = type.GetPropertyList();
            List <string>       ColumnDisplayOrderList = ColumnDisplayOrder.SplitString(",").ToList();
            List <string>       PropertyList           = new List <string>();

            foreach (PropertyInfo PI in PropertyArr)
            {
                if (PI.CanRead)
                {
                    PropertyList.Add(PI.Name);
                }
            }
            if (ColumnDisplayOrder.Length > 0)
            {
                foreach (string ColumnName in ColumnDisplayOrderList)
                {
                    if (ColumnName.In(PropertyList))
                    {
                        reVal.Columns.Add(ColumnName);
                    }
                }
            }

            if (reVal.Columns.Count == 0)
            {
                foreach (string PIN in PropertyList)
                {
                    reVal.Columns.Add(PIN);
                }
            }
            foreach (T InsertModel in InputList)
            {
                DataRow DW = reVal.NewRow();
                foreach (string PIN in PropertyList)
                {
                    DW[PIN] = InsertModel.GetObjectPropertyValue(PIN);
                }
                reVal.Rows.Add(DW);
            }
            return(reVal);
        }