IList怎么转换成DataTable

yuji821 2011-06-19 08:24:35
在公司写过的,忘记怎么写了

DataTable dt = null;
if (lstHt != null && lstHt.Count > 0)
{
dt = new DataTable();
foreach (string s in lstHt[0].Keys)
{
dt.Columns.Add(new DataColumn(s, typeof(string)));
}
foreach (Hashtable ht in lstHt)
{
//这里不知道怎么写
}
}
return dt;
...全文
358 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Moqin89 2011-11-14
  • 打赏
  • 举报
回复
有没高手出现!
子夜__ 2011-06-19
  • 打赏
  • 举报
回复
有这种现成的方法
/// <summary>
/// 获取列名集合
/// </summary>
private IList<string> GetColumnNames(DataColumnCollection dcc)
{
IList<string> list = new List<string>();
foreach (DataColumn dc in dcc)
{
list.Add(dc.ColumnName);
}
return list;
}

/// <summary>
///属性名称和类型名的键值对集合
/// </summary>
private Hashtable GetColumnType(DataColumnCollection dcc)
{
if (dcc == null || dcc.Count == 0)
{
return null;
}
IList<string> colNameList = GetColumnNames(dcc);

Type t = typeof(T);
PropertyInfo[] properties = t.GetProperties();
Hashtable hashtable = new Hashtable();
int i = 0;
foreach (PropertyInfo p in properties)
{
foreach (string col in colNameList)
{
if (col.ToLower().Contains(p.Name.ToLower()))
{
hashtable.Add(col, p.PropertyType.ToString() + i++);
}
}
}

return hashtable;
}

/// <summary>
/// DataTable转换成IList
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public IList<T> ToList(DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
{
return null;
}

PropertyInfo[] properties = typeof(T).GetProperties();//获取实体类型的属性集合
Hashtable hh = GetColumnType(dt.Columns);//属性名称和类型名的键值对集合
IList<string> colNames = GetColumnNames(hh);//按照属性顺序的列名集合
List<T> list = new List<T>();
T model = default(T);
foreach (DataRow dr in dt.Rows)
{
model = new T();//创建实体
int i = 0;
foreach (PropertyInfo p in properties)
{
if (p.PropertyType == typeof(string))
{
p.SetValue(model, dr[colNames[i++]], null);
}
else if (p.PropertyType == typeof(int))
{
p.SetValue(model, int.Parse(dr[colNames[i++]].ToString()), null);
}
else if (p.PropertyType == typeof(DateTime))
{
p.SetValue(model, DateTime.Parse(dr[colNames[i++]].ToString()), null);
}
else if (p.PropertyType == typeof(float))
{
p.SetValue(model, float.Parse(dr[colNames[i++]].ToString()), null);
}
else if (p.PropertyType == typeof(double))
{
p.SetValue(model, double.Parse(dr[colNames[i++]].ToString()), null);
}
}

list.Add(model);
}

return list;
}




原文参考
yuji821 2011-06-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liuchaolin 的回复:]

自己改吧,但List《实体>应该没有改成DataTable的必要吧,List也可以做成datalist的数据源直接邦定
public DataTable ToADODataTable()
{
DataTable dt = new DataTable();
Type type = base.GetType();
dt.TableName = type.Name;
……
[/Quote]
我就是要转换成DataTable,按照我的要求做
md5e 2011-06-19
  • 打赏
  • 举报
回复
自己改吧,但List《实体>应该没有改成DataTable的必要吧,List也可以做成datalist的数据源直接邦定
public DataTable ToADODataTable()
{
DataTable dt = new DataTable();
Type type = base.GetType();
dt.TableName = type.Name;
dt.Namespace = type.Namespace;
Type elementType = this.GetElementType();
AddColumns(dt, elementType);
PropertyInfo[] properties = elementType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
int length = properties.Length;
object[] values = new object[length];
foreach (SAPStructure structure in this)
{
for (int i = 0; i < length; i++)
{
values[i] = properties[i].GetValue(structure, null);
}
dt.Rows.Add(values);
}
return dt;
}




暖枫无敌 2011-06-19
  • 打赏
  • 举报
回复

foreach (DictionaryEntry ht in lstHt)
{
DataRow dr = dt.NewRow();
dr[ht.Key] = ht.Value;
dt.Rows.Add(dr);
}
threenewbee 2011-06-19
  • 打赏
  • 举报
回复
dt.Rows.Add(new string[] { ht.Key, ht.Value });

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧