Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有来自IDataReader的列表数据的AutoMapper 使用(IDataReader dr=DatabaseContext.ExecuteReader(命令)) { if(dr.Read()) { AutoMapper.Mapper.CreateMap(); 返回AutoMapper.Mapper.Map(dr); } 返回null; }_C#_Automapper_Idatareader - Fatal编程技术网

C# 带有来自IDataReader的列表数据的AutoMapper 使用(IDataReader dr=DatabaseContext.ExecuteReader(命令)) { if(dr.Read()) { AutoMapper.Mapper.CreateMap(); 返回AutoMapper.Mapper.Map(dr); } 返回null; }

C# 带有来自IDataReader的列表数据的AutoMapper 使用(IDataReader dr=DatabaseContext.ExecuteReader(命令)) { if(dr.Read()) { AutoMapper.Mapper.CreateMap(); 返回AutoMapper.Mapper.Map(dr); } 返回null; },c#,automapper,idatareader,C#,Automapper,Idatareader,如果dr只有一行,则错误:引发了类型为“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”的异常 如果dr的行数超过一行,则运行ok 有什么帮助吗?问题是Automapper也在调用Read(),所以要一直查看第二条记录。如果您想一想,如果读卡器中有1000行,AutoMapper如何将其转换为一个列表,而不反复调用Read() 把你的电话改成叫HasRows e、 g 使用(IDataReader dr=DatabaseContext.

如果dr只有一行,则错误:引发了类型为“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”的异常

如果dr的行数超过一行,则运行ok


有什么帮助吗?

问题是Automapper也在调用Read(),所以要一直查看第二条记录。如果您想一想,如果读卡器中有1000行,AutoMapper如何将其转换为一个列表,而不反复调用Read()

把你的电话改成叫HasRows

e、 g

使用(IDataReader dr=DatabaseContext.ExecuteReader(命令))
{
如果(哈斯罗博士)
{
AutoMapper.Mapper.CreateMap();
返回AutoMapper.Mapper.Map(dr);
}
返回null;
}

添加AutoMapper.Net4并在CreateMap之前添加映射器,如下所示:

using (IDataReader dr = DatabaseContext.ExecuteReader(command))
    {
        if (dr.HasRows)
        {
            AutoMapper.Mapper.CreateMap<IDataReader, ProductModel>();
            return AutoMapper.Mapper.Map<IDataReader, IList<ProductModel>>(dr);
        }

        return null;
    }

谢谢你,但是。。。AutoMapper.Mapper.CreateMap();或AutoMapper.Mapper.CreateMap();是吗?我看到他们两个都工作得很好!我总是喜欢说得更明确些,这样我就更清楚了。它可能有一些魔力,所以它在内部并不重要。它抛出一个错误,表示它不喜欢
IList
。当我指定
list
而不是
IList
时,它会将空列表返回给我。这是原始问题的答案吗?
using (IDataReader dr = DatabaseContext.ExecuteReader(command))
    {
        if (dr.HasRows)
        {
            AutoMapper.Mapper.CreateMap<IDataReader, ProductModel>();
            return AutoMapper.Mapper.Map<IDataReader, IList<ProductModel>>(dr);
        }

        return null;
    }
    MapperRegistry.Mappers.Add(new DataReaderMapper());
    MapperRegistry.Mappers.Add(new NameValueCollectionMapper());
    MapperRegistry.Mappers.Add(new HashSetMapper());
    MapperRegistry.Mappers.Add(new ListSourceMapper());
    MapperRegistry.Mappers.Add(new TypeConverterMapper());