您的位置:首页 > 其它

关于DataTable与IList和List泛型集合的相互转换在网上总结

2011-07-29 11:59 627 查看
我在做amchart的还有微软相关的chart控件时发现绑定使用Datatabale不能绑定 但是支持DataSet 和泛型集合;于是谢谢网上好友的帮助;自己做了下总结

自己弄了一些集合转化的文章;

对于技术方面的理论我不需多言;

主要是是通过映射命名空间;使用Linq的相关查询;和Type类获取列名;使用泛型转化为实体类后放到集合:

代码如下:

publicstaticclass ConvertTolistInfo
{
///<summary>
/// DataTable 转换为List 集合
///</summary>
///<typeparam name="T">类型</typeparam>
///<param name="dt">DataTable</param>
///<returns></returns>
publicstatic List<TResult> ToList<TResult>(this DataTable dt) where TResult : class,new()
{
List<PropertyInfo> prlist =new List<PropertyInfo>();
Type t =typeof(TResult);
Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) !=-1)prlist.Add(p); });
List<TResult> objlist =new List<TResult>();
foreach (DataRow row in dt.Rows)
{
TResult obj =new TResult();
prlist.ForEach(p =>
{
if (row[p.Name] != DBNull.Value)
p.SetValue(obj, row[p.Name], null);
});
objlist.Add(obj);
}
return objlist;
}
///<summary>
/// 转换为一个DataTable
///</summary>
///<typeparam name="T"></typeparam>
///<param name="value"></param>
///<returns></returns>
publicstatic DataTable ToDataTable<T>(this IEnumerable<T> value) where T : class
{
List<PropertyInfo> plist =new List<PropertyInfo>();
Type t =typeof(T);
//T type = new T();
//Type columen = type.GetType();这种方式与 where T :new()这种方式使用 否则不适用;
DataTable dt =new DataTable();
Array.ForEach<PropertyInfo>(t.GetProperties(), p =>
{
plist.Add(p);
dt.Columns.Add(p.Name, p.PropertyType);
});
foreach (var item in value)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
plist.ForEach(p => row[p.Name] = p.GetValue(item, null));
dt.Rows.Add(row);
}
return dt;
}
}


  二,构造泛型类:

/// <summary>
/// 实体转换辅助类
/// </summary>
publicclass ModelConvertHelper<T>where T : new()
{
publicstatic IList<T> ConvertToModel(DataTable dt)
{
// 定义集合
IList<T> ts =new List<T>();

// 获得此模型的类型
Type type =typeof(T);

string tempName ="";

foreach (DataRow dr in dt.Rows)
{
T t =new T();

// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();

foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;

// 检查DataTable是否包含此列
if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue;

object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}

ts.Add(t);
}
return ts;
}

/// <summary>          
/// 提供将DataTable类型对象转换为List集合          
/// </summary>          
/// <param name="table"></param>          
/// <returns></returns>
publicstatic List<T> ConvertToList<T>(DataTable table) where T : new()
{
//置为垃圾对象
List<T> list =null;
if (table !=null)
{
DataColumnCollection columns = table.Columns;
int columnCount = columns.Count;
T type =new T();
Type columnType = type.GetType();
PropertyInfo[] properties = columnType.GetProperties();
if (properties.Length == columnCount)
{
list =new List<T>();
foreach (DataRow currentRow in table.Rows)
{
for (int i =0; i < columnCount; i++)
{
for (int j =0; j < properties.Length; j++)
{
if (columns[i].ColumnName == properties[j].Name)
{ properties[j].SetValue(type, currentRow[i], null); }
}
}
list.Add(type); type =new T();
}
}
else { list =null; }
}
else
{
thrownew ArgumentNullException("参数不能为空");
}
return list;
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: