using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
|
namespace IStation.Extensions
|
{
|
/// <summary>
|
/// DataTable拓展
|
/// </summary>
|
public static class DataTableExtension
|
{
|
/// <summary>
|
/// 转化为可枚举对象
|
/// </summary>
|
public static IEnumerable<T> ToEnumerable<T>(this DataTable dt) where T : new()
|
{
|
foreach (DataRow dr in dt.Rows)
|
{
|
T t = new T();
|
PropertyInfo[] properties = t.GetType().GetProperties();
|
foreach (PropertyInfo pro in properties)
|
{
|
//判断DataTable是否包含此列
|
if (dt.Columns.Contains(pro.Name))
|
{
|
//判断此属性是否有Setter
|
if (!pro.CanWrite)
|
continue;
|
object value = dr[pro.Name];
|
if (value != DBNull.Value)
|
pro.SetValue(t, value, null);
|
}
|
}
|
yield return t;
|
}
|
}
|
|
|
}
|
|
}
|