DataTable.Rows.Find()找不到行

本文关键字:找不到 Rows Find DataTable | 更新日期: 2023-09-27 18:11:12

我正在尝试搜索一个数据表的行,我知道存在。

// This is the row my search should find
DataRow goal = dtLkupCat.Rows[6];
// This finds the row correctly
string srchexpr = String.Format("sport = '{0}' and catcode = '{1}' and type = '{2}' and [parent] = '{3}' and code = '{4}'", goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"]);
DataRow[] test = dtLkupCat.Select(srchexpr);
// But if I set a PK and search for the values I know to be correct, it returns null
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
                                          dtLkupCat.Columns["catcode"],
                                          dtLkupCat.Columns["type"],
                                          dtLkupCat.Columns["parent"],
                                          dtLkupCat.Columns["code"]};
DataRow lkup = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });

它搜索的列/值没有什么特别的。它们都是有效字符串,没有null/DBNull。我遗漏了什么?显然,我可以使用Select()作为解决方案,但想知道为什么Find()不起作用。

更新:如果有人想尝试一下,我已经从我的查找表的一个子集发布了xml。你可以从:http://www.flantech.net/files/lkup_cat2.zip

下载

然后尝试运行此代码。奇怪的是,它会发现该行使用了四列的不同组合,但绝不会使用所有五列。

DataTable dtLkupCat = new DataTable("lkup_cat");
dtLkupCat.ReadXml(@"lkup_cat2.xml");
// This is the row my search should find
DataRow goal = dtLkupCat.Rows[0];
// This is how I need to do the search, but it doesn't find the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
                                          dtLkupCat.Columns["catcode"],
                                          dtLkupCat.Columns["type"],
                                          dtLkupCat.Columns["parent"],
                                          dtLkupCat.Columns["code"]};
DataRow found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "sport" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["catcode"],
                                          dtLkupCat.Columns["type"],
                                          dtLkupCat.Columns["parent"],
                                          dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["catcode"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "catcode" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
                                          dtLkupCat.Columns["type"],
                                          dtLkupCat.Columns["parent"],
                                          dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["type"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");
// Here I remove the "type" column from the PK, and it finds the row
dtLkupCat.PrimaryKey = new DataColumn[] { dtLkupCat.Columns["sport"],
                                          dtLkupCat.Columns["catcode"],
                                          dtLkupCat.Columns["parent"],
                                          dtLkupCat.Columns["code"]};
found = dtLkupCat.Rows.Find(new object[] { goal["sport"], goal["catcode"], goal["parent"], goal["code"] });
Debug.WriteLine((found == null ? "not " : "") + "found");

DataTable.Rows.Find()找不到行

尝试将最后一行更改为以下内容:

             DataRow lkup = dtLkupCat.Rows.Find(new object[] 
        { 
            goal["sport"].ToString(),
            goal["catcode"].ToString(), 
            goal["type"].ToString(), 
            goal["parent"].ToString(), 
            goal["code"].ToString() 
        });

假设您的值都是字符串

Per MS:

感谢您报告这个问题。我们调查了这个问题,这是我们代码中的一个bug。由于修复此问题将导致破坏性更改,我们需要仔细评估何时以及如何修复此问题,以便将其对现有客户的负面影响降到最低。我们已经有一个现有的连接错误跟踪这个问题(http://connect.microsoft.com/VisualStudio/feedback/details/491319/dataset-designer-generates-invalid-datarelations)。我会将此错误解决为连接错误#491319的"副本",但我们会在取得进展时向您更新。同时,作为一种解决方法,您可以在设置主键之前将PK设置为null,在XML上设置约束以匹配代码上新PK的顺序,或者在设置PK并执行Find之前清除约束和关系。

https://connect.microsoft.com/VisualStudio/feedback/details/694803/datatable-rows-find-fails-to-locate-row

我遇到了同样的问题,并通过显式调整新对象的大小来解决这个问题。found = dtLkupCat.Rows.Find(new object[5] { goal["sport"], goal["catcode"], goal["parent"], goal["code"] });