public static List <T> ToList <T>(this ExcelReference range, Func <T> factory = null) where T : class
        {
            var items = new List <T>();

            XLObjectMapper.AddRange(items, range.GetValue(), factory);
            return(items);
        }
Esempio n. 2
0
        /// <summary>
        /// Directly load the the rows from a dbreader into a list of xlobjects
        /// Important: The order of the returned columns must match the one of T column mapping
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static List <T> ToXLObjectList <T>(this DbDataReader reader) where T : class
        {
            var  list = new List <T>();
            Type t    = typeof(T);
            var  map  = XLObjectMapper.GetObjectMapping <T>(null);

            while (reader.Read())
            {
                T instance = (t.GetConstructor(Type.EmptyTypes) != null) ? (T)Activator.CreateInstance(t, new object[0])
                            : Activator.CreateInstance <T>();
                int nFields = reader.FieldCount;

                for (int i = 0; i < nFields; i++)
                {
                    map.SetColumn(instance, i, reader.GetValue(i));
                }
                list.Add(instance);
            }
            return(list);
        }