#region DataTable To List/Model
/// <summary>
/// DataTable To List
/// </summary>
/// <typeparam>object type</typeparam>
/// <param>DataTable</param>
/// <returns>return a List Model type</returns>
public static List<T> DataTableToObjectList<T>(DataTable dt) where T : new()
{
DataRowCollection drc = dt.Rows;
int columncount = drc.Count;
List<T> result = new List<T>(); //declare the generic type of return
Type type = typeof(T);
PropertyInfo[] propertys = type.GetProperties(BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public|BindingFlags.SetProperty); //get the collections of the model
foreach (DataRow r in drc)
{
result.Add(DataRowToObjectModel<T>(r, propertys));
}
return result;
}
/// <summary>
/// DataRow To a Model
/// </summary>
/// <typeparam>the type of Model</typeparam>
/// <param>DataRow</param>
/// <param>the object to Model</param>
/// <returns>return a Model Type</returns>
private static T DataRowToObjectModel<T>(DataRow r, PropertyInfo[] propertys) where T : new()
{
T t = new T();
for (int i = 0; i < propertys.Length; i++)
{
object obj = r[propertys[i].Name];
if (obj != null)
{
if (propertys[i].PropertyType == typeof(int))
propertys[i].SetValue(t, PublicMethod.GetInt(obj), null);
if (propertys[i].PropertyType == typeof(string))
propertys[i].SetValue(t, obj.ToString(), null);
if (propertys[i].PropertyType == typeof(DateTime))
propertys[i].SetValue(t, PublicMethod.GetDateTime(obj), null);
}
}
return t;
}
#endregion
分页存储过程。
复制代码 代码如下: