c# - how to compare two objects from two different contexts -
i have 2 in application contexts, both equal in schematic & structure, 1 on remote server , other local server installed on computer. assumed both databases same different data, try data both contexts comparison , can have same data in both databases. use distinct extension method not work same. class called users , attributes are: id, name, last name, user, password. id field identity not null. have identical record in both databases, therefore, use distinct should not return data returned them. checked hash gethashcode method , these different. here code:
foreach (var item in remote.users) lstusers.add(item); foreach (var item in local.users) lstusers.add(item); var results = lstusers.distinct().tolist();
you cannot use default implementation of gethashcode() tell if 2 objects have same field values. if different object instances (which are), gethashcode() return different values each object.
in event, since each database has id identity not null, id different objects otherwise identical.
there few options, of may better or weaker depending on exact needs
- use uniqueid (guid) instead of integer id, , use comparison.
- add unique identifier separate id each record, , use comparison.
- implement iequatable.
i cautious overriding gethashcode() , equals().
Comments
Post a Comment