IList及泛型集合类转换DataTable
来源:互联网 发布:苹果电脑平面设计软件 编辑:程序博客网 时间:2024/05/19 21:15
http://hi.baidu.com/blueyund/blog/item/e1d108f7dc51cb2b720eec9b.html
/// <summary>
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
/// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="list">集合</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list)
{
return ConvertX.ToDataTable<T>(list, null);
}
/// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="list">集合</param>
/// <param name="propertyName">需要返回的列的列名</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
{
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == 0)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == 0)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
- IList及泛型集合类转换DataTable
- IList及泛型集合类转换DataTable
- IList及泛型集合类转换DataTable
- 将DataTable转换成泛型集合IList<>
- 把IList和泛型数组转换为DataTable
- 把IList和泛型数组转换为DataTable
- 把IList和泛型数组转换为DataTable
- DataTable转换为IList
- DataTable转换成IList
- DataTable转换成IList
- DataTable转换成IList
- IList转换Datatable
- DataTable 转换 泛型集合
- DataTable转换成IList(二)
- ILIST<t>转换成DataTable
- DataTable转换成IList 【转载】
- 泛型集合、datatable常用数据类型转换Json帮助类
- 将 IList 或 IList 转换为Dataset或DataTable
- java学习
- 无语,发现了ShellExecuteA和ShellExecuteW 以及ShellExecuteExW 之间的一些小“秘密”
- WireShark点点滴滴
- 线程
- c#中如何实现自动按下键盘上的某个键
- IList及泛型集合类转换DataTable
- Emacs 配置文件
- Cisco大型局域网方案(一)
- 引用 arm2410和44b0启动文件分析(很全面)
- 往往把一些基本的给忽视了
- 3COM小型办公室有线局域网方案
- 转帖 支付宝接口程序、文档及解读(ASP.NET) 學習
- 我自由了
- 做项目时遇到的几个关于C#和SQL的细节问题(二)