【发布时间】:2013-02-26 16:30:25
【问题描述】:
我正在尝试从 Linq 查询中选择一个仅包含指定字段的数字 DataRows,然后使用这些 DataRows 填充 DataTable。问题是,当我将这些 DataRows 添加到新的 DataTable 时,我希望 ID 和 Name 字段都被分别填充。但是,DataTable 中的 ID 字段同时包含 ID 和 Name 值。有人可以指出我做错了什么。
代码如下:
var query2 = from s in Tables[Table_Sec].AsEnumerable()
where query.Contains(s["sectype"])
select new { id = s["id"], name = s["name"] }; // I only want these fields
DataTable dt = new DataTable(); // Create my new dataTable
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
foreach(var row in query2)
{
dt.Rows.Add(row); // ID field contains both ID and Name strings. Name field contains nothing
}
【问题讨论】:
-
出了什么问题?你能更清楚地描述问题,你有例外吗?
query是什么?