views:

32

answers:

2

hi, i want to remove repeated record's from results but distinct don't do this for me! why???

var results = (from words in _Xplorium.Words
                           join wordFiles in _Xplorium.WordFiles on words.WordId equals wordFiles.WordId
                           join files in _Xplorium.Files on wordFiles.FileId equals files.FileId
                           join urls in _Xplorium.Urls on files.UrlId equals urls.UrlId
                           where files.Title.Contains(query) || files.Description.Contains(query)
                           orderby wordFiles.Count descending                               
                           select new SearchResultItem()
                           {
                               Title = files.Title,
                               Url = urls.Address,
                               Count = wordFiles.Count,
                               CrawledOn = files.CrawledOn,
                               Description = files.Description,
                               Lenght = files.Lenght,
                               UniqueKey = words.WordId + "-" + files.FileId + "-" + urls.UrlId
                           }).Distinct();
+1  A: 

You may have to implement your own IEqualityComparer for SearchResultItem.

You can then pass that to Distinct and force it to compare using your code. That way you ensure the comparison is being done how you want.

Justin Niessner
+1  A: 

Assuming SearchResultItem is a class and not a struct, then as a reference type, it is only truly "equal" if it is a reference to the same type. But, you have created a new object for each result. All your values will be considered distinct unless:

  • You can pass in your own IEqualityComparer object. Or,
  • you can have your class implement IEquatable, and override the default GetHashCode and Equals properties, to make them equal if all the values are the same (or whatever criteria is approproate).
Patrick Karcher
first option is better and simpler! thanks
Sadegh
If that will be the only comparison, or that comparison is different than usual, yes. If there really is something that makes the objects "equal" every time, and this comparison happens repeatedly, then the 2nd is great.
Patrick Karcher