泛型类:
public class MoudeType
{
public string RGuid { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string State { get; set; }
public string Sys { get; set; }
public string OPCServiceGuid { get; set; }
public string BIMItemGuid { get; set; }
}
将datatable中的值赋值给泛型
for (int i = 0; i < dtNew.Rows.Count; i++)
{
MoudeTypeItem = new MoudeType()
{
Name = Convert.ToString(dtNew.Rows[i]["部件名称"]),
Type = Convert.ToString(dtNew.Rows[i]["部件类型"]),
State = "开",
Sys = Convert.ToString(dtNew.Rows[i]["所属系统"]),
RGuid = Convert.ToString(dtNew.Rows[i]["RowGuid"])
};
#region 根据设备RowGuid在表DICT_BIMOPC 中获取
// MoudeTypeItem.OPCServiceGuid = dtNew.Rows[i]["OPCServiceGuid"].ToString();
// MoudeTypeItem.BIMItemGuid = dtNew.Rows[i]["BIMItemGuid"].ToString();
#endregion
ListMoude.Add(MoudeTypeItem);
}
//将泛型数据转换为 DataTable
DataTable dt = CheckAllOrUn.ToDataTable(ListMoude);
转换的方法
#region 将IList 转换成DataTable
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;
}
#endregion