/// <summary>
/// Dataset 集合根据传入的 类型。自动转换List集合"
/// </summary>
/// <typeparam name="T">类(属性类 modle)</typeparam>
/// <param name="ds">数据集合</param>
/// <returns>List集合</returns>
public List<T> GetListbyDataSet<T>(DataSet ds) where T : new()
{
List<T> li = new List<T>(); //声明要返回的集合
var s = typeof(T); // 获取传入类型
var str = s.GetProperties(); // 获取传入类型的属性集合
if (ds.Tables[0] == null || ds.Tables[0].Rows.Count < 0) //判断ds的null和是否包含数据
{
return li;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) //循环集合准备获取数据
{
T t1 = new T(); // 声明类
foreach (var item in str) // 循环类的属性
{
string itemstr = item.Name; //类属性名称
var itemtype = item.PropertyType; // 类属性的类型(int string datetime)
object value = GetvalbyDataSet(itemstr, itemtype, ds.Tables[0].Rows[i]); //获取值
item.SetValue(t1, value, null);
}
li.Add(t1);
}
return li;
}
/// <summary>
/// 在DataRow中 获取 对应列的值
/// </summary>
/// <param name="colname">列名称</param>
/// <param name="colname">列的类型</param>
/// <param name="dr">DataRow 集合</param>
/// <returns>列值</returns>
private object GetvalbyDataSet(string colname, Type coltype, DataRow dr)
{
if (dr.Table.Columns.Contains(colname))
{
if (typeof(decimal?) == coltype)
{
return dr[colname] == null ? 0 : Convert.ToDecimal(dr[colname].ToString());
}
if (typeof(decimal)==coltype)
{
return dr[colname] == null ? 0 : Convert.ToDecimal(dr[colname].ToString());
}
if (typeof(bool) == coltype)
{
return dr[colname] == null ? false : Convert.ToBoolean(dr[colname].ToString());
}
if (typeof(int?) == coltype)
{
//return dr[colname] == null ? 0 : int.Parse(dr[colname].ToString());
return dr[colname] == null ? 0 : Convert.ToInt32(dr[colname].ToString());
}
if (typeof(int) == coltype)
{
return dr[colname] == null ? 0 : int.Parse(dr[colname].ToString());
}
if (typeof(DateTime) == coltype)
{
return dr[colname] == null ? DateTime.Parse("2016/9/22") : DateTime.Parse(dr[colname].ToString());
}
if (typeof(decimal) == coltype)
{
return dr[colname] == null ? decimal.Parse("0") : decimal.Parse(dr[colname].ToString());
}
string str = dr[colname] == null ? "" : dr[colname].ToString();
return str;
}
else
{
if (typeof(decimal?) == coltype)
{
return decimal.Parse("0");
}
if (typeof(decimal) == coltype)
{
return decimal.Parse("0");
}
if (typeof(bool) == coltype)
{
return false;
}
if (typeof(int?) == coltype)
{
return 0;
}
if (typeof(int) == coltype)
{
return 0;
}
if (typeof(DateTime) == coltype)
{
return null;
}
if (typeof(decimal) == coltype)
{
return 0;
}
else
{
return "";
}
}
}