C#.NET IDataReader扩展 ToList<T>/Get<T>
2010-08-16 21:02
453 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Data; using System.Data.Common; using System.Web.Script.Serialization; using System.IO; using System.Security.Cryptography; namespace Pub.Class { public static class IDataReaderExtensions { public static List<TResult> ToList<TResult>(this IDataReader dr) where TResult : class,new() { List<TResult> list= new List<TResult>(); if (dr == null) return list; int len = dr.FieldCount; while (dr.Read()) { TResult info = new TResult(); for (int j = 0; j < len; j++) { if (dr[j] == null || string.IsNullOrEmpty(dr[j].ToString())) continue; System.Reflection.PropertyInfo pi = info.GetType().GetProperty(dr.GetName(j).Trim()); pi.SetValue(info, dr[j], null); } list.Add(info); } dr.Close(); dr.Dispose(); dr = null; return list; } public static T Get<T>(this IDataReader reader, string field) { return reader.Get<T>(field, default(T)); } public static T Get<T>(this IDataReader reader, string field, T defaultValue) { var value = reader[field]; if(value == DBNull.Value) return defaultValue; return value.ConvertTo<T>(defaultValue); } } }
调用实例:
public virtual IList<EC_Type> SelectListByAll(string order, string fieldList, bool useCache) { string cacheKey = string.Format("EC_TypeCache_SelectListByAll_{0}_{1}", order, fieldList); IList<EC_Type> list = null; if (useCache) { list = (IList<EC_Type>)Cache2.Get(cacheKey); if (list != null) return list; } DbParameter[] dbParams = { Data.MakeInParam("@Fields", (DbType)SqlDbType.VarChar, 1000, fieldList), Data.MakeInParam("@Order", (DbType)SqlDbType.VarChar, 100, order), }; list = Data.GetDbDataReader("EC_Type_SelectListByAll", dbParams).ToList<EC_Type>(); if (useCache) Cache2.Insert(cacheKey, list, cacheSeconds); return list; }
相关文章推荐
- [转载]C#.NET DataTable扩展 ToList/Get/OrderBy/Where/ToPage
- C#.NET DataTable扩展 ToList/Get/OrderBy/Where/ToPage
- 用python模仿c#的List<>一组list筛选扩展方法
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- C#.Net:List<T>集合列表的Linq语句查询示例
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- IDataReader/DataTable扩展ToList 改进版,反射速度慢的惊人!
- c#List泛型数据扩展,把List<>型数据格式化成List<SelectListItem>,用来作dropdownlist的数据
- 扩展方法 DataTable的ToList<T>
- js Array to C# List<Dictionary>(js 传参 自定义类列表 到C#)
- c#.net如何将API返回json对象转换成自己想要的List<T>
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx 关于JSON对象解析List<XXX>在调用时出错问题
- 扩展方法 DataTable To List<T>
- 深入理解 c# 第一章 使用扩展方法对 List<Product> 排序
- C#.NET DateTime扩展 GetTimeSpan/ToDateTime/CalculateAge/ToAgo/DateDiff
- <From C++ To C#> 再来说说.NET Framework的跨语言性