C# list 和dataTable 互转


1. 数据表DataTable 转 List<> 

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace List与DataTable互转
{
/// <summary>
/// datatable转list
/// </summary>
/// <typeparam name="T"></typeparam>
public class DtToList<T> where T : new()
{
/// <summary>
/// datatable转list
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToModel(DataTable dt)
{
List<T> ts = new List<T>();// 定义集合
Type type = typeof(T); // 获得此模型的类型
PropertyInfo[] propertys = type.GetProperties();
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;
if (dt.Columns.Contains(tempName))
{
if (!pi.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> List<T>(DataTable dt)
{
var list = new List<T>();
Type t = typeof(T);
var plist = new List<PropertyInfo>(typeof(T).GetProperties());

foreach (DataRow item in dt.Rows)
{
T s = System.Activator.CreateInstance<T>();
for (int i = 0; i < dt.Columns.Count; i++)
{
PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
if (info != null)
{
if (!Convert.IsDBNull(item[i]))
{
info.SetValue(s, item[i], null);
}
}
}
list.Add(s);
}
return list;
}

}
}

 

2. List 转化DataTable 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace List与DataTable互转
{
public class ConvertHelper
{
/// <summary>
/// list转datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <returns></returns>
public static DataTable ListToDt<T>(IEnumerable<T> collection)
{
var props = typeof(T).GetProperties();
var dt = new DataTable();
dt.TableName = typeof(T).Name;
dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
if (collection.Count() > 0)
{
for (int i = 0; i < collection.Count(); i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in props)
{
object obj = pi.GetValue(collection.ElementAt(i), null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
dt.LoadDataRow(array, true);
}
}
return dt;
}

 

}
}

3 . 建立一个与数据表对应的实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace List与DataTable互转
{
public class StudentEntity
{
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }

/// <summary>
/// 年纪
/// </summary>
public int Age { get; set; }
}
}

 

4 . 具体演示

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace List与DataTable互转
{
class Program
{
static void Main(string[] args)
{
//Type t = typeof(StudentEntity);
//PropertyInfo[] propertys = t.GetProperties();
////PropertyInfo property = propertys.Where(find => find.Name=="Name") as PropertyInfo;
////PropertyInfo property1 = propertys.Where(find => find.Name == "Name") as PropertyInfo;
////PropertyInfo property1 = propertys.Select(find => find.Name == "Name") as PropertyInfo;

//PropertyInfo property = propertys.ToList().Find(a => a.Name == "Name");

//Console.WriteLine(property.Name);
List<StudentEntity> data=new List<StudentEntity>();
data.Add(new StudentEntity(){Name = "aaa",Age = 12});
data.Add(new StudentEntity() { Name = "bbb", Age = 12 });
data.Add(new StudentEntity() { Name = "ccc", Age = 12 });

DataTable dt = ConvertHelper.ListToDt(data);


Console.Read();
}
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM