0

So I am generating a list of top rated titles pulled from the db, and returning the list with the rating. The issue I am having though is that it is returning duplicates. I can use another list to compare and exclude titles but that will only leave a couple as the initial function takes(5). As I am not sure where to prevent it in the initial function was hoping someone could help me. Thanks

This is what I have pulling the Top5 Titles:

public List<MediaRating> IndividualTopFive()
{
    var topFiveList = db.MediaRatingData
                            .Include(u => u.Title)
                            .Where(u => u.RateFor.UserName == User.Identity.Name)
                            .OrderByDescending(i => i.MediaRatingID)
                            .OrderByDescending(r => r.Rating)
                            .Take(5)
                            .ToList();
    return topFiveList;
Abstract3000
  • 79
  • 1
  • 4
  • 15
  • are you sure that the data not duplicated in the database? – Feras Salim May 14 '16 at 08:27
  • There is in fact duplicated data in the database, (there's a reason behind that) but I am just trying to sift the top 5 different titles. I am considering just throwing the entire thing into a for loop that counts to 5, adds to a list and takes(1) adds that to the list and ensures the next title doesn't match on the following iteration... seems a bit ghetto though. I was thinking there maybe something simpler that could be added to the ".OrderByDescending(i => i.MediaRatingID)" line that I'm unaware of. – Abstract3000 May 14 '16 at 08:32
  • 1
    see this http://stackoverflow.com/a/489421/2374987 – Feras Salim May 14 '16 at 08:41

1 Answers1

0

First note it is likely that you have duplicates in your database, so maybe you should simply delete them there in the first place.

If you are sure that you want to remove duplicates from the result of your query, then depending on how you MediaRating class looks like, you can use Linq's Distinct method or DistictBy from MoreLinq (or from John Skeet's post).

Note that Distinct method might reorder your elements (it depends on Linq provider's implementation), so you should call it before sorting. On the other hand, you can use John Skeet's DistinctBy before .Take(5) to get appropriate results since it is guaranteed to preserve the order of elements.

Community
  • 1
  • 1
Tomasz Maczyński
  • 973
  • 10
  • 24
  • Thank you, I was just reading up on the .Distinct() that returns an IEnumerable but I already have the .ToList() in there so I'm gonna give it a shot. – Abstract3000 May 14 '16 at 08:36
  • 1
    @Abstract3000 you should use Distinct before sorting (since it does not guarantee to preserve order), or you can use John Sheet's DistinctBy before ".Take(5)" to get appropriate results. If you look at DistinctBy's implementation, you'll notice that it preserves the order of elements. – Tomasz Maczyński May 14 '16 at 08:54
  • tried adding .Distinct() at the top before any of the sorting and that didn't fix it so looking into John Sheets method now. – Abstract3000 May 14 '16 at 09:14