您的位置:首页 > 编程语言 > C#

C# List集合转换成DataTable

2014-02-05 16:08 495 查看
    /// <summary>
/// 将List集合 转换成 DataTable
/// </summary>
/// <param name="entitys">sellerSearchDealList是我自己定义的一个类</param>
public static DataTable ListToTable(List<sellerSearchDealList> entitys)
{
DataTable dtresult = new DataTable();
if (entitys == null || entitys.Count < 1)
{
throw new Exception("空");
}
else
{
PropertyInfo[] propertys = entitys[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
dtresult.Columns.Add(pi.Name, pi.PropertyType);
}

for (int i = 0; i < entitys.Count; i++)
{
ArrayList tenpList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(entitys[i], null);
tenpList.Add(obj);
}
object[] array = tenpList.ToArray();
dtresult.LoadDataRow(array, true);
}
}
return dtresult;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: