【问题标题】:LINQ query to Data Table returns incorrect resultsLINQ 查询数据表返回不正确的结果
【发布时间】:2013-03-27 11:33:14
【问题描述】:

我想对 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();

     }       

提前感谢

【问题讨论】:

  • 你的数据表里有什么内容?
  • 只有两列 ID 和 Names
  • @user2102572 我说的是内容,不是结构:)
  • 看不到问题中的内容。 CL 在哪里填充?同样发布,可能在填充数据表时出错。
  • 您没有发布全部代码,DataTable 是如何填充的?在您点击查询并使用您的内容更新问题之前,您能否在调试中查看 DataTable。您发布的内容将得到一个IEnumerable&lt;string&gt;,其中每个都是该列的值。有人会怀疑长度值在Name 列中。

标签: c# linq datatable


【解决方案1】:

我怀疑 Value 正在返回您不怀疑的值。

首先,尝试:

for (int i = 0; i< dataGridView1.Rows.Count; i++)
{
  rt = CL.NewRow();
  string value1 = dataGridView1.Rows[i].Cells[0].Value;
  string value2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
  //Breakpoint here, check the values of value1 and value2
  CL.Rows.Add(rt);
}

我也会尝试您的查询的不同变体。

string[] names = dt.Rows.Cast<DataRow>().Where(row => row["Id"] == 1).Select(row => row["Name"].ToString()).ToArray();

然后在此时检查名称。请评论答案第一部分的结果以查看值是什么。

【讨论】:

  • 先生。首先,当我在特定行设置断点时,卢克首先引用 id=1,即使没有 ToString();
  • 顺便说一句,我应用了您的查询,它只是绑定名为“Length”的列而没有任何行
  • value1value2的值分别是多少?
【解决方案2】:
    private void btnShow(object sender, EventArgs e)
{
    DataTable CL = new DataTable();
    DataRow rt;
    DataTable dt = new DataTable();
    DataRow row;
    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);
    }

    IEnumerable<DataRow> results = from myRow in CL.AsEnumerable()
                                   where myRow.Field<string>("ID") == "1"
                                   select myRow;

            foreach (var re in results)
            {
                row = dt.NewRow();
                dt.Rows.Add(st.Field<string>("Name"));
            }
   dataGridView2.DataSource = dt;

 }    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多