我想对 Datatable 进行 LINQ 查询以选择具有特定 ID 的名称,但它返回名称的长度而不是字符串,这里是一些示例代码:
private void btnShow(object sender, EventArgs e)
{
DataTable CL = new DataTable();
DataRow rt;
CL.Columns.Add(new System.Data.DataColumn("ID", typeof(string)));
CL.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
for (int i = 0; i< dataGridView1.Rows.Count; i++)
{
rt = CL.NewRow();
rt[0] = dataGridView1.Rows[i].Cells[0].Value.ToString();
rt[1] = dataGridView1.Rows[i].Cells[1].Value.ToString();
CL.Rows.Add(rt);
}
var results = from myRow in CL.AsEnumerable()
where myRow.Field<string>("ID") == "1"
select myRow.Field<string>("Name").ToString();
dataGridView2.DataSource = results.ToList();
}
提前谢谢