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); }
/// <summary> /// Reader转成T的集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="type"></param> /// <param name="dr"></param> /// <param name="fields"></param> /// <param name="isClose"></param> /// <param name="isTry"></param> /// <returns></returns> internal static List <T> DataReaderToList <T>(Type type, IDataReader dr, string fields, bool isClose = true, bool isTry = true) { if (type.Name.Contains("KeyValuePair")) { List <T> strReval = new List <T>(); FillValueTypeToDictionary(type, dr, strReval); return(strReval); } //值类型 else if (type.IsValueType || type == SqlSugarTool.StringType) { List <T> strReval = new List <T>(); FillValueTypeToDr <T>(type, dr, strReval); return(strReval); } //数组类型 else if (type.IsArray) { List <T> strReval = new List <T>(); FillValueTypeToArray(type, dr, strReval); return(strReval); } var cacheManager = CacheManager <IDataReaderEntityBuilder <T> > .GetInstance(); string key = "DataReaderToList." + fields + type.FullName; IDataReaderEntityBuilder <T> eblist = null; if (cacheManager.ContainsKey(key)) { eblist = cacheManager[key]; } else { eblist = IDataReaderEntityBuilder <T> .CreateBuilder(type, dr); cacheManager.Add(key, eblist, cacheManager.Day); } List <T> list = new List <T>(); try { if (dr == null) { return(list); } while (dr.Read()) { list.Add(eblist.Build(dr)); } if (isClose) { dr.Close(); dr.Dispose(); dr = null; } } catch (Exception ex) { if (isClose) { dr.Close(); dr.Dispose(); dr = null; } Check.Exception(true, "错误信息:实体映射出错。\r\n错误详情:{0}", ex.Message); } return(list); }