简体   繁体   English

DataTable不合并用户定义的对象

[英]DataTable does not merge user defined objects

I got a problem with a DataTable handling an object defined by myself (Position). 我在处理自己定义的对象的数据表时遇到了问题(位置)。 The object stores some data and overwrites ToString, Equals and GetHashCode. 该对象存储一些数据并覆盖ToString,Equals和GetHashCode。 Further it provides the static operators for == and !=. 此外,它为==和!=提供了静态运算符。

The DataTable works fine as long the column storing this object is not a key column. 只要存储此对象的列不是键列,DataTable即可正常工作。 Setting it as a PrimaryKey and merge another table to it fails, because the content will not found equal. 将其设置为PrimaryKey并将其合并到另一个表失败,因为找不到内容相等。 (Suppricing the datagridview and the DataTable itself finds the data violating the key after merging.) (为datagridview和DataTable提供价格,使其在合并后发现违反键的数据。)

Attached you'll find some manual comparison of the data after merging. 附件中,您将在合并后找到一些数据的手动比较。

Has anybody an idea how to deal with that? 有人知道如何处理吗?

Best regards, Tilo 最好的问候,提洛

this.data.Rows[1][5]==this.data.Rows[0][5]
false

(Position)this.data.Rows[1][5]==(Position)this.data.Rows[0][5]
true

this.data.Rows[1][5].Equals(this.data.Rows[0][5])
true

this.data.Rows[1][5].GetHashCode()
-461267399
this.data.Rows[0][5].GetHashCode()
-461267399

this.data.Columns[5]
{PositionX}
base {System.ComponentModel.MarshalByValueComponent}: {PositionX}
AllowDBNull: false
AutoIncrement: false
AutoIncrementSeed: 0
AutoIncrementStep: 1
Caption: "PositionX"
ColumnMapping: Element
ColumnName: "PositionX"
DataType: {Name = "Position" FullName = "TiloW.Parameter.Position"}
DateTimeMode: UnspecifiedLocal
DefaultValue: {}
Expression: ""
ExtendedProperties: Count = 1
MaxLength: -1
Namespace: ""
Ordinal: 5
Prefix: ""
ReadOnly: false
Table: {}
Unique: false 

Just for information. 仅供参考。 I found a solution adding a new column storing the overriden hashcode which will be used for merging and internal things. 我找到了一个解决方案,添加了一个新列,用于存储覆盖的哈希码,该哈希码将用于合并和内部操作。 It will be deleted just before export and created during import or generation. 它将在导出之前删除,并在导入或生成期间创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM