IDataReader扩展ToList<T> 改进版
// 最快 和直接循环差不多
public static List < TResult > ToList < TResult > ( this IDataReader dr, bool isClose) where TResult : class , new () {
IDataReaderEntityBuilder < TResult > eblist = IDataReaderEntityBuilder < TResult > .CreateBuilder(dr);
List < TResult > list = new List < TResult > ();
if (dr == null ) return list;
while (dr.Read()) list.Add(eblist.Build(dr));
if (isClose) { dr.Close(); dr.Dispose(); dr = null ; }
return list;
}
public static List < TResult > ToList < TResult > ( this IDataReader dr) where TResult : class , new () {
return dr.ToList < TResult > ( true );
}
// 也慢
public static List < TResult > ToList2 < TResult > ( this IDataReader dr, bool isClose) where TResult : class , new () {
List < TResult > oblist = new List < TResult > ();
if (dr == null ) return oblist;
List < string > drColumns = new List < string > ();
int len = dr.FieldCount;
for ( int j = 0 ; j < len; j ++ ) drColumns.Add(dr.GetName(j).Trim());
List < PropertyInfo > prlist = new List < PropertyInfo > ();
Type t = typeof (TResult);
Array.ForEach < PropertyInfo > (t.GetPropertiesCache(), p => { if (drColumns.IndexOf(p.Name) != - 1 ) prlist.Add(p); });
while (dr.Read()) {
TResult ob = new