开发者

how to remove repeated record's from results linq to sql

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();


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.


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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜