static Delegate CreatePrimativeMapFunc(Type typeT, IReadOnlyCollection <Column> columns) { Contract.Requires(typeT != null); Contract.Requires(columns != null); Contract.Requires(columns.Count > 0); var col0 = columns.First(); if (!Types.AreCompatible(col0.Type, typeT)) { Mapping._trace.OnNext($"Cannot map column {col0.Name} to {typeT} as column type {col0.Type} is not compatible"); } var readerParam = Expression.Parameter(typeof(DbDataReader), "reader"); var resultParam = Expression.Parameter(typeT, "result"); bool readAsObject; var getMethod = DataReaderGetMethod(col0.Type, out readAsObject); Expression value = Expression.Call(readerParam, getMethod, Expression.Constant(0)); if (readAsObject) { value = Expression.Convert(value, col0.Type); } if (col0.Type != typeT) { value = ConvertOrCastValue(value, col0.Type, typeT); } var body = Expression.Condition( Expression.IsTrue(Expression.Call(readerParam, typeof(DbDataReader).GetMethod("IsDBNull", new[] { typeof(int) }), Expression.Constant(0))), Expression.Default(typeT), value); var func = typeof(Func <,>).MakeGenericType(new[] { typeof(DbDataReader), typeT }); return(Expression.Lambda(func, body, new[] { readerParam }).Compile()); }