1. ホーム
  2. リスト

IEnumerableとDataTableの互換性

2022-02-23 19:44:49
  /// <summary>
        /// Converting a DataTable to a List collection
        /// </summary>
        /// <typeparam name="TResult">type</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns></returns>
        public static List<TResult> ToList<TResult>(DataTable dt) where TResult : class, new()
        {
            // Create a list of properties
            List<PropertyInfo> prlist = new List<PropertyInfo>();
            //Get an instance of the type of TResult Entry point for reflection
            Type t = typeof(TResult);
            //Get all the Public properties of TResult and find out which TResult property has the same name as the DataTable's column (PropertyInfo) and add it to the property list 
            Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) ! = -1) prlist.Add(p); });
            // Create the returned collection
            List<TResult> oblist = new List<TResult>();

            foreach (DataRow row in dt.Rows)
            {
                // Create an instance of TResult
                TResult ob = new TResult();
                //find the corresponding data and assign it a value
                prlist.ForEach(p => { if (row[p.Name] ! = DBNull.Value) p.SetValue(ob, row[p.Name], null); });
                //put into the returned collection.
                oblist.Add(ob);
            }
            return oblist;
        }

        /// <summary>
        //// Convert to a DataTable
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(IEnumerable list) 
        {
            // Create a collection of properties
            List<PropertyInfo> pList = new List<PropertyInfo>();
            //Get the entry point for reflection
            Type type = list.AsQueryable().ElementType;
            DataTable dt = new DataTable();
            // Add all the public properties to the collection and add the columns of the DataTable
            Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
            foreach (var item in list)
            {
                // Create a DataRow instance
                DataRow row = dt.NewRow();
                //assign a value to row
                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                // Add to DataTable
                dt.Rows.Add(row);
            }
            return dt;
        }


        /// <summary>
        //// Convert to a DataTable
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<TResult>(IEnumerable<TResult> value) where TResult : class
        {
            // Create a collection of properties
            List<PropertyInfo> pList = new List<PropertyInfo>();
            //Get the entry point for reflection
            Type type = typeof(TResult);
            DataTable dt = new DataTable();
            //add all the public properties to the collection and add the columns of the DataTable
            Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
            foreach (var item in value)
            {
                // Create a DataRow instance
                DataRow row = dt.NewRow();
                //assign a value to row
                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                // Add to DataTable
                dt.Rows.Add(row);
            }
            return dt;
        }