/// <summary> /// 从DataRow读取数据并映射到范型T这里要求T必须有一个默认的无参构造函数 /// 如: /// var cmdt = @"select p.BlocNo AS B_BlocNo from S_UIFilterFieldPlan p left join S_UIFilterFields f on p.SUIFilterFieldID=f.SUIFilterFieldID"; /// var row = DBHelper.ExecuteDataRow(cmdt, Provider); /// var obj = DataMapping.ToModel(S_UIFilterFieldPlan)(row, s => new Dictionary(object,string) { { s.UIFilterFields, "B_" } }); /// </summary> /// <typeparam name="T">要映射的数据类型T</typeparam> /// <param name="reader">System.Data.DataRow</param> /// <param name="expression">指定有外键关系时外键表使用的别名的一个表达式</param> /// <returns>映射之后的实例</returns> public static T ToModel <T>(DataRow row, Expression <Func <T, object> > expression) where T : new() { var obj = new T(); ModelBindingProxy proxy = new ModelBindingProxy(typeof(T), expression); proxy.InitBinding(row.Table); return((T)proxy.Bind(obj, row)); }
public static object ToModel(object obj, IDataReader reader, ModelBindingProxy proxy) { proxy.InitBinding(reader); using (reader) { if (reader.Read()) { return(proxy.Bind(obj)); } } return(null); }
/// <summary> /// 从reader读取数据并映射到范型T这里要求T必须有一个默认的无参构造函数 /// 如: /// var cmdt = @"select p.BlocNo AS B_BlocNo from S_UIFilterFieldPlan p left join S_UIFilterFields f on p.SUIFilterFieldID=f.SUIFilterFieldID"; /// var reader = DBHelper.ExecuteReader(cmdt, Provider); /// var obj = DataMapping.ToModel(S_UIFilterFieldPlan)(reader, s => new Dictionary(object, string) { { s.UIFilterFields, "B_" } }); /// </summary> /// <typeparam name="T">要映射的数据类型T</typeparam> /// <param name="reader">IDataReader</param> /// <param name="expression">指定有外键关系时外键表使用的别名的一个表达式</param> /// <returns>映射之后的实例</returns> public static T ToModel <T>(IDataReader reader, Expression <Func <T, object> > expression) where T : new() { var obj = new T(); ModelBindingProxy proxy = new ModelBindingProxy(typeof(T), expression); proxy.InitBinding(reader); using (reader) { if (reader.Read()) { return((T)proxy.Bind(obj)); } } return(default(T)); }