,因为标题建议我试图将dataReader对象映射到对象列表中,这是我使用的代码,我尝试使用autoMapper 4.2.1和3.3尝试过。1
Mapper.Initialize(cfg =>
{
cfg.CreateMap<IDataReader, MemberChangesDto>()
.ForMember(dest => dest.MemberNo, opt => opt.MapFrom(src => (string)src["Member_No"]))
.ForMember(dest => dest.OrganisationName, opt => opt.MapFrom(src => (string)src["Member_Name"]))
.ForMember(dest => dest.MemberJoinDate, opt => opt.MapFrom(src => (string)src["Membership_StartDate"]))
.ForMember(dest => dest.ResignationDate, opt => opt.MapFrom(src => (string)src["Membership_Resigned_Date"]))
.ForMember(dest => dest.MemberStatusId, opt => opt.MapFrom(src => (string)src["Membership_Status_Code"]));
}
);
初始化汽车应用程序后,我会使用
对其进行验证Mapper.AssertConfigurationIsValid();
这没有返回错误,然后我称映射
IDataReader dataTableReader = memberChangesDS.Tables[2].CreateDataReader();
IList<MemberChangesDto> members = Mapper.Map<IList<MemberChangesDto>>(dataTableReader);
但是,这返回错误
{"缺少类型的映射配置或不支持的映射。 映射类型: idatareader-> iList
1 System.Data.IDataReader -> System.Collections.Generic.IList
1 [[成员Changesdto,模型, 版本= 1.0.0.0,文化=中性, publicKeyToken = null]]] 目标路径: ilist'1 源值:system.data.datatablereader"}
我试图映射的对象看起来像这样。
public class MemberChangesDto
{
public string MemberNo { get; set; }
public string OrganisationName { get; set; }
public string MemberJoinDate { get; set; }
public string ResignationDate { get; set; }
public string MemberStatusId { get; set; }
}
任何人都可以将我指向正确的方向,我已经使用了从对象到对象的自动应用程序,但是我认为idatareader在这里引起了问题。
预先感谢。
此方法创建来自SQL查询的对象实例,而不是使用AutoMapper:
public IEnumerable<T> FillDataRows<T>(String query, params SqlParameter[] sqlParams) {
var properties = typeof(T).GetProperties().ToList();
IList<T> result = new List<T>();
var source = FillDataRows(query, sqlParams).ToList();
var firstInSource = source.FirstOrDefault();
if (firstInSource == null)
return result;
//remove properties not exist in source
properties.RemoveAll(p => firstInSource.Table.Columns.Contains(p.Name) == false);
foreach (var row in source) {
var item = createItemFromRow<T>((DataRow)row, properties);
result.Add(item);
}
return result;
}
private T createItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) {
T item = Activator.CreateInstance<T>();
foreach (var property in properties) {
if (row[property.Name] != System.DBNull.Value)
property.SetValue(item, row[property.Name], null);
}
return item;
}
public void FillDataSet(DataSet ds, String dataTable, String query, params SqlParameter[] sqlParams) {
using (var cn = new SqlConnection(this.connectionString)) {
cn.Open();
using (var cmd = cn.CreateCommand()) {
cmd.CommandTimeout = commandTimeout;
cmd.CommandText = query;
if (sqlParams != null && sqlParams.Length > 0)
cmd.Parameters.AddRange(sqlParams);
using (var adapter = new System.Data.SqlClient.SqlDataAdapter(cmd)) {
adapter.Fill(ds, dataTable);
}
}
cn.Close();
}
}
public IEnumerable<DataRow> FillDataRows(String query, params SqlParameter[] sqlParams) {
var ds = new DataSet();
FillDataSet(ds, "Result", query, sqlParams);
return ds.Tables["Result"].Rows.OfType<DataRow>();
}
样本:
var sql = "select MemberNo = Member_No, OrganisationName = Member_Name ... From MYTABLES where Member_No = @ID";
var results = FillDataRows<MemberChangesDto>(sql, new SqlParameter("@ID", 12345));