/// <summary> /// 映射下一项 /// </summary> /// <param name="result"></param> /// <returns></returns> public bool MapNext(out T result) { if (source.Next()) { object value = source[0]; try { if (value == DBNull.Value || value == null) { if (underlyingType != null) { result = default(T); return(true); } } result = (T)Convert.ChangeType(source[0], underlyingType ?? typeof(T)); } catch (FormatException e) { throw new InvalidCastException($"列{source.Columns[0]}的值“{value}”无法转换为{typeof(T)}.", e); } catch (InvalidCastException e) { throw new InvalidCastException($"列{source.Columns[0]}的值“{value}”无法转换为{typeof(T)}.", e); } return(true); } result = default(T); return(false); }
/// <summary> /// 映射下一结果 /// </summary> /// <returns></returns> public bool MapNext(out TEntity result) { if (!source.Next()) { result = default(TEntity); return(false); } else if (!initialized) { Initialize(); } result = New(); foreach (var descriptor in mappers) { var columnIndex = descriptor.Key; var description = descriptor.Value; try { description.Map(result, source, columnIndex, converts[columnIndex]); } catch (Exception e) { throw new InvalidCastException($"{description.MemberName} 列映射失败,值“{source[columnIndex]}”对于类型 {description.MemberValueType} 无效", e); } } return(true); }