C# DataTable.Rows.Find()未找到行

C# DataTable.Rows.Find()未找到行,c#,.net-3.5,C#,.net 3.5,我正在尝试在数据表中搜索我知道存在的行 // 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}'"

我正在尝试在数据表中搜索我知道存在的行

// 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。您可以从以下网站下载:

然后试着运行这个代码。奇怪的是,它会发现行使用了四列的不同组合,但从来没有使用所有五列

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

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

             DataRow lkup = dtLkupCat.Rows.Find(new object[] 
        { 
            goal["sport"].ToString(),
            goal["catcode"].ToString(), 
            goal["type"].ToString(), 
            goal["parent"].ToString(), 
            goal["code"].ToString() 
        });
假设您的值都是每毫秒的字符串:

谢谢你报道这个问题。我们调查了这个问题,它是我们代码中的一个bug。由于解决此问题将导致重大变化,我们需要仔细评估何时以及如何解决此问题,以便将对现有客户造成的负面影响降至最低。我们已经有一个跟踪此问题的现有连接错误。我将解决这个错误作为“重复”的连接错误491319,但我们将保持您的最新进展。 同时,作为一种解决方法,您可以在设置主键之前将PK设置为null,使XML上的约束与代码上新PK的顺序相匹配,或者在设置PK之前清除约束和关系并执行Find


我不知道答案,但我在做一些猜测。一种可能是列没有很好地定义。也许您也可以调用DataSet.FillSchema。另一种可能是有两行具有相同的主键集。尝试检查test.Length。还要检查dtLkupCat.Columns[xyz]是否为null.test.Length为1,如果键列中存在重复项或null,则在设置PK时会引发错误。