检查 2 个数据表是否具有相同的架构
我想确保 2 个 Ado.net 数据表具有相同的架构:列数 + col 类型等。如何做到这一点?
假设我有变量:数据表 A 和数据表 B。如何比较 A 的架构与 B 的架构是否相同
I want to make sure that 2 Ado.net datatables have the same schema: number of columns + col types etc. How can this be done?
Lets say I have variables: Datatable A and Datatable B. How can I compare to see if the schema of A is same as Schema of B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的项目所采用的方法。我只测试了我关心的属性的相等性。如果您关心的不仅仅是这里的内容,您可以添加其余的属性。请注意,这里的其他答案都没有验证主键是否匹配,而我的却验证了。
This is the method I went with for my project. I only tested the equality of the properties I cared about. You could add on the remaining properties if you care about more than what's here. Note that none of the other answers here verify the primary keys match up, whereas mine does.
我不知道有什么内置方法可以比较数据表,并且没有定义完整的规范(而且您总是应该)我可能会错过您关心的某些案例。
也就是说,以下确实设法比较两个数据表并确定以下内容是否为真
它使用扩展方法并实现 IEqualityComparer 来进行比较。
测试用例
扩展方法
IEqualityComparer的实现
输出
I don't know of any built in way to compare DataTables and without having defined a complete specification (and you always should) its likely that I'm going to miss some case you care about.
That said the following does manage to compare two DataTables and determine if the following is true
It's using an Extension Method and implements IEqualityComparer to make the comparison.
Test Cases
Extension Method
Implementation of IEqualityComparer
Output
康拉德的回答绝对有帮助。但我使用下面的方式来比较数据表结构。
The answer from Conrad was definitely helpful. But I used the below way to compare the datatable structure.