views:

28

answers:

2

I'm using Entity Framework 4.0. I want to be able to find duplicate records in an EntitySet. The records will have the same data but different primary keys. When I do a .Equals I get that the records are not equal. I know I can override .Equals but I've got over 20 related entities each with a lot of fields.

Is there a compare method available that will look at all the fields except the key to tell me if they're the same?

Seems like this would be a common issue and I can't imagine it hasn't been solved already.

Thanks for any advice...

A: 

Instead of overriding the .Equals() method, you can use its overload to specify your own IEqualityComparer...

Also, if you just want the de-duped or distinct values, you can use the .Distinct() method, or you can do a LINQ group-by query to isolate criteria that has more than one match...

var sample = from a in sampleA join b in sampleB 
on a.SampleProperty equals b.SampleProperty 
into c select c.FirstOrDefault();

http://msdn.microsoft.com/en-us/library/ms132151.aspx

fdfrye
Thanks fdfrye, I've considered both of these approaches. Unfortunately they're too cumbersome. I've got over 400 separate fields to comare over 20 related tables. I'm looking for something that can compare the entire aggregated Entity with its 20 related tables or at least something that can take a single related table and compare it. The snafu is that each record will have a unique key which makes it different.
A: