示例#1
0
        private static T _getItem <T>(IDataReader dr, List <PropertyMapping> allMappings) where T : new()
        {
            var item = new T();

            foreach (PropertyMapping mapping in allMappings)
            {
                var prop = mapping.Property;
                if (prop == null)
                {
                    continue;
                }

                if (mapping.PropertyMapperDataRow != null)
                {
                    var rowValues = new DataValueList();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        object value = dr.IsDBNull(i) ? null : dr[i];
                        rowValues.Add(new DataValue {
                            ColumnName = dr.GetName(i), Value = value
                        });
                    }
                    var mapperResult = mapping.PropertyMapperDataRow(rowValues);
                    prop.SetValue(item, mapperResult, null);
                    continue;
                }

                //skip if there is no data column
                if (mapping.OrdinalIndex == -1)
                {
                    continue;
                }

                if (dr.IsDBNull(mapping.OrdinalIndex))
                {
                    prop.SetValue(item, null, null);
                    continue;
                }

                var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;

                if (type.GetTypeInfo().IsEnum)
                {
                    var value = Enum.ToObject(type, dr[mapping.OrdinalIndex]);
                    prop.SetValue(item, value, null);
                }
                else
                {
                    var value = Convert.ChangeType(dr[mapping.OrdinalIndex], type);
                    prop.SetValue(item, value, null);
                }
            }

            return(item);
        }
示例#2
0
 /// <summary>
 /// Converts a DataReader to a strongly typed IEnumerable.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dr"></param>
 /// <param name="mapperFunction">Function that returns an instance of T passing in the current row.</param>
 public static IEnumerable <T> GetList <T>(IDataReader dr, Func <DataValueList, T> mapperFunction) where T : class, new()
 {
     while (dr.Read())
     {
         var rowValues = new DataValueList();
         for (int i = 0; i < dr.FieldCount; i++)
         {
             object value = dr.IsDBNull(i) ? null : dr[i];
             rowValues.Add(new DataValue {
                 ColumnName = dr.GetName(i), Value = value
             });
         }
         var item = mapperFunction(rowValues);
         yield return(item);
     }
 }
示例#3
0
        /// <summary>
        /// Async version to convert SqlDataReader to strongly typed List (only supports SQL Server at the moment)
        /// </summary>
        public static async Task <List <T> > GetListAsync <T>(SqlDataReader dr, Func <DataValueList, T> mapperFunction) where T : class, new()
        {
            var list = new List <T>();

            while (await dr.ReadAsync())
            {
                var rowValues = new DataValueList();
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    object value = dr.IsDBNull(i) ? null : dr[i];
                    rowValues.Add(new DataValue {
                        ColumnName = dr.GetName(i), Value = value
                    });
                }
                var item = mapperFunction(rowValues);
                list.Add(item);
            }
            return(list);
        }