比较两个数据行
我有一个数据表,其中包含多行。我还有 1 行,我想检查该行是否与数据表中现有行重复。所以我尝试了这样的操作:
DataTable dataTable = GetTable();
if (dataTable.Rows.Count > 1)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var dataRow = dataTable.Rows[i];
if (dt.Rows.Contains(dataRow) && dt.Rows.Count != 0) // Giving error
continue;
dt.ImportRow(dataRow);
return dataRow;
}
}
在这里,我的 dataTable 也第一次可以为 null/空。
但其给出的错误为:
表没有主键。
任何人都可以帮助我吗?如果需要额外的代码,只需注释即可。
I have a datatable with multiple rows inside it. I have got 1 more row and I want to check if this row is a duplicate of the existing rows in the datatable. So I tried like:
DataTable dataTable = GetTable();
if (dataTable.Rows.Count > 1)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
var dataRow = dataTable.Rows[i];
if (dt.Rows.Contains(dataRow) && dt.Rows.Count != 0) // Giving error
continue;
dt.ImportRow(dataRow);
return dataRow;
}
}
Here, my dataTable
can also be null/empty for the first time.
But its giving error as:
Table doesn't have a primary key.
Can anyone help me please. If additional code is required, just comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能在 DataTable 对象上添加 PK 吗?
我认为代码会是这样的:
Can't you add the PK on your DataTable object?
I think the code would be something like this:
我假设您的
dt
变量应该是您的dataTable
变量,如果您收到错误消息,告诉您您的表没有主键,可能是因为您使用了错误的变量,并且确实没有主键或与您尝试使用的变量关联的表。所以我认为代码应该看起来像这样
I assume that your
dt
variable is supposed to be yourdataTable
variable, if you are getting an error telling you that your table doesn't have a primary key it could be because you are using the wrong variable and that there really isn't a primary key or a table associated with the variable that you are trying to use.so I assume that the code should look like this instead