Remove Duplicate Titles from list in MVC
This question already has an answer here:
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.
下一篇: 从MVC列表中删除重复标题