如何轻松地将DataReader转换为List< T&gt ;? [英] How can I easily convert DataReader to List<T>?

查看:193
本文介绍了如何轻松地将DataReader转换为List< T&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 DataReader 中有数据,我想将其转换为 List< T>
有什么可能的简单解决方案?

I have data in a DataReader which I want to be converted to a List<T>. What is a possible simple solution for this?

例如在CustomerEntity类中,我具有CustomerId和CustomerName属性。如果我的DataReader将这两列作为数据返回,那么如何将其转换为 List< CustomerEntity>

For e.g. in CustomerEntity class, I have CustomerId and CustomerName properties.If my DataReader returns these two columns as data, then how can I convert it into List<CustomerEntity>.

推荐答案

我已经看到系统使用反射和属性或字段上的属性将DataReader映射到对象的系统。 (有点像LinqToSql所做的事情。)它们节省了一些键入操作,并且可以减少为DBNull等编码时的错误数量。一旦缓存了生成的代码,它们也比大多数手写代码还快,所以如果要做很多,请考虑高速公路。

I have seen systems that use Reflection and attributes on Properties or fields to maps DataReaders to objects. (A bit like what LinqToSql does.) They save a bit of typing and may reduce the number of errors when coding for DBNull etc. Once you cache the generated code they can be faster then most hand written code as well, so do consider the "high road" if you are doing this a lot.

请参见。NET中的反射防御 就是其中一个示例。

See "A Defense of Reflection in .NET" for one example of this.

然后您可以编写类似

class CustomerDTO  
{
    [Field("id")]
    public int? CustomerId;

    [Field("name")]
    public string CustomerName;
}

...

using (DataReader reader = ...)
{    
   List<CustomerDTO> customers = reader.AutoMap<CustomerDTO>()
                                    .ToList();
}

(AutoMap()是扩展方法)

(AutoMap(), is an extension method)

@Stilgar,感谢您的伟大评论

@Stilgar, thanks for a great comment

如果能够,您可能会更好地使用 NHibernate,EF或Linq to Sql等,但是在旧项目中(或出于其他(有时是有效的)原因,例如此处未发明,对存储过程的喜爱等),并非总是可能使用ORM,因此重量较轻的系统对于袖手旁观很有用。

If are able to you are likely to be better of using NHibernate, EF or Linq to Sql, etc However on old project (or for other (sometimes valid) reasons, e.g. "not invented here", "love of stored procs" etc) It is not always possible to use a ORM, so a lighter weight system can be useful to have "up your sleeves"

如果您也需要编写很多IDataReader循环,那么您会发现好处无需更改正在使用的系统的体系结构即可减少编码(和错误)的方法。

If you every needed too write lots of IDataReader loops, you will see the benefit of reducing the coding (and errors) without having to change the architecture of the system you are working on. That is not to say it’s a good architecture to start with..

我假设CustomerDTO不会脱离数据访问层,并且将构建复合对象等。

I am assuming that CustomerDTO will not get out of the data access layer and composite objects etc will be built up by the data access layer using the DTO objects.

我写了这个答案几年后 Dapper 进入了.NET的世界,这可能是编写onw AutoMapper的一个很好的起点,也许它将完全不需要您这样做。

A few years after I wrote this answer Dapper entered the world of .NET, it is likely to be a very good starting point for writing your onw AutoMapper, maybe it will completely remove the need for you to do so.

这篇关于如何轻松地将DataReader转换为List&lt; T&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆