/// <summary>
        ///     Convert a List{T} to a DataTable.
        /// </summary>
        public static DataTable ToDataTable <T>(List <T> items)
        {
            var tb = new DataTable(typeof(T).Name);

            var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var prop in props)
            {
                var t = MetaDataHelper.GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }

            foreach (var item in items)
            {
                var values = new object[props.Length];

                for (var i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return(tb);
        }
Beispiel #2
0
 public void SetType(IEnumerable source)
 {
     if (Type == null)
     {
         Type = MetaDataHelper.GetEnumerableItemType(source);
         SetProperties(Type);
     }
 }
Beispiel #3
0
        private DataTable ExtendTable(DataTable table, Type type)
        {
            // Extend the table schema if the input table was null or if the value
            // in the sequence is derived from type T.
            foreach (var p in GetProperties(type))
            {
                if (!OrdinalMap.ContainsKey(p.Name))
                {
                    // Add the property as a column in the table if it doesn't exist
                    // already.
                    var dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
                                                        : table.Columns.Add(p.Name, MetaDataHelper.GetCoreType(p.PropertyType));

                    // Add the property to the ordinal map.
                    OrdinalMap.Add(p.Name, dc.Ordinal);
                }
            }

            // Return the table.
            return(table);
        }