使用Hashtable比较C#代码中的数据库表行 [英] Compare database table rows in c# code using Hashtable

查看:109
本文介绍了使用Hashtable比较C#代码中的数据库表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较c#中的表记录.我决定用Hashtable来做.我应该添加所有列值并在Hashtable中添加键,然后将字符串进行比较以检测差异还是有更好的方法?

I need to compare table records in c#. I have decided to do it with Hashtable. Should I append all the column values and add it with a key to Hashtable then compare the strings together to detect difference or there is a better way?

推荐答案

附加所有列值"-听起来不太正确.

创建一个表示行数据的类,不要将列数据转换为字符串,如果它是一个日期,请使用DateTime,如果它的bigint要使用long,等等.

实现 IComparable [ SortedSet [
"Should I append all the column values" - doesn''t sound quite right.

Create a class representing the row data, don''t convert column data to strings, if it''s a date use DateTime, if its a bigint use long, and so on.

Implement the IComparable[^] on your row class.

Create a class implmenting IComparer<> for your class - That''s easy when you have implemented IComparable.

Create a SortedSet[^] - you will not be allowed to add duplicates.

Or do as Christian advices - perform the operations using SQL in the database.

Best regards
Espen Harlinn


我不确定为什么您决定"使用哈希表,然后再决定是否可以执行所需的操作.我认为这取决于您所说的表"的含义.如果您的意思是SQL表,而您只在寻找完全匹配的内容,那么最好的选择是在数据库中而不是在C#代码中进行.
I''m not sure why you ''decided'' to use a hashtable before deciding if that will do what you want. I would think it depends on what you mean by ''table''. If you mean SQL tables and you''re looking only for exact matches, your best bet is to do that in the database, not in C# code.


在基于数据库的索引上添加索引在最重要的领域.然后按该顺序检索记录将确保将相同的记录一起返回.

通过该键将结果分组,然后比较所有剩余字段,您将找到所有重复项.
Add an index on your database based on the most important fields. Then retreiving records in that order will ensure that identical record are returned together.

Group the result by that key and and then compare all remainding fields and you will find all duplicates.


这篇关于使用Hashtable比较C#代码中的数据库表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆