DataSet:Enumerator 和 FindById 不返回相等的 DataRow
今天我的代码中的问题有点奇怪,我还无法重现它。 我正在使用类型化数据集(由设计器创建),并且循环遍历数据表中的所有行。
有时(!),当通过主键查找时,返回的行不等于枚举器中的行。 这是我为重现该问题而编写的一些代码:
foreach(DataSet1.DataTable1Row dr in ds.DataTable1)
{
if(ds.DataTable1.FindById(dr.Id) != dr)
Console.Write(dr.Id);
}
不会将任何行写入控制台,因为 FindById 始终返回同一行,这确实符合逻辑。 在我的项目代码中,具有一些字符串列的类似数据集,在大约 3% 的行中(总是相同的行!)它没有,并且其中一个字符串字段只是空的:
ds.DataTable1.FindById(dr.Id) != dr // returns false, for whatever reason
主键是唯一的主键字段,因此 FindById 是一个生成方法。 有人知道一点提示或者以前遇到过同样的问题吗? 恐怕这是我造成的一个非常非常特殊的情况,导致了这个错误或功能。
我想到了这可能是由枚举完成的转换产生的。 枚举器确实适用于生成的类型化行的 DataRow
基本类型。 但我并没有发现什么不对劲的地方...
Today's problem in my code is kind of strange, and I could not reproduce it yet. I'm working with a typed dataset (created with the designer) and I'm looping over all rows in a datatable.
Sometimes (!), when finding via primary key, the returned row is not equal to the one from the enumerator. This is some code I wrote to reproduce the issue:
foreach(DataSet1.DataTable1Row dr in ds.DataTable1)
{
if(ds.DataTable1.FindById(dr.Id) != dr)
Console.Write(dr.Id);
}
No line will be written to the console, because FindById always returns the same row, which is really logical. In my project's code, with a similar dataset with a few String columns, in about 3% of the rows (always the same rows!) it doesn't, and one of the String fields is just empty:
ds.DataTable1.FindById(dr.Id) != dr // returns false, for whatever reason
The primary key is the only primary key field, and therefor FindById is a generated method. Does anybody know a little hint or did experience the same problem before? I'm afraid it's a very very special case I made that enables this bug or feature.
I thought about the possibility of this being produced by the cast done by the enumeration. The enumerator does work with the DataRow
base type of the generated typed rows. But I didn't find something wrong there...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的主键是什么类型? DataSet 在比较 Guid(以及可能的其他值)时有一个微妙的错误。 Guid 错误仅与某些 Guid 值有关,并且通常效果很好。
注意:当我说有错误时,我的意思是我知道错误报告已被接受,但我不知道它是否也已修复
WHat's the type of you primary key? the DataSet has a subtle bug for comparing Guids (and possibly other values). The Guid error only has to do with certain Guid values and usually works well.
note: When I say have a bug, I mean I know that a bug report has been accepted but I don't know if it's been fix as well