How can I remove an "ALMOST" Duplicate using LINQ? ( OR SQL? )
- by Atomiton
This should be and easy one for the LINQ gurus out there.
I'm doing a complex Query using UNIONS and CONTAINSTABLE in my database to return ranked results to my application.
I'm getting duplicates in my returned data. This is expected. I'm using CONTAINSTABLE and CONTAINS to get all the results I need. CONTAINSTABLE is ranked by SQL and CONTAINS (which is run only on the Keywords field ) is hard-code-ranked by me. ( Sorry if that doesn't make sense )
Anyway, because the tuples aren't identical ( their rank is different ) a duplicate is returned.
I figure the best way to deal with this is use LINQ.
I know I'll be using the Distinct() extension method, but do I have to implement the IEqualityComparer interface? I'm a little fuzzy on how to do this.
For argument's sake, say my resultset is structured like this class:
class Content {
ContentID int //KEY
Rank int
Description String
}
If I have a List<Content> how would I write the Distinct() method to exclude Rank? Ideally I'd like to keep the Content's highest Rank. SO, if one Content's RAnk is 112 and the other is 76. I'd like to keep the 112 rank.
Hopefully I've given enough information.