DataTable.Rows.Find()找不到行

时间:2011-10-13 03:24:03

标签: c# .net-3.5

我正在尝试在DataTable中搜索我知道存在的行。

// 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");

2 个答案:

答案 0 :(得分:0)

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

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

假设您的值都是字符串

答案 1 :(得分:0)

Per MS:

  

感谢您报告此问题。我们调查了这个问题,这是我们代码中的一个错误。由于修复问题将导致重大变化,我们需要仔细评估何时以及如何解决此问题,以便对现有客户造成最小的负面影响。我们已经有一个跟踪此问题的现有Connect错误(http://connect.microsoft.com/VisualStudio/feedback/details/491319/dataset-designer-generates-invalid-datarelations)。我会将此错误解决为Connect错误#491319的“重复”,但我们将在您取得进展时随时更新。   同时作为一种解决方法,您可以在设置主键之前将PK设置为null,使XML上的约束与代码上新PK的顺序相匹配,或者在设置PK并执行查找之前清除约束和关系。

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