EF Code First - Linq to Entities Union EqualityComparer

I have two IEnumerable collections that I would like to combine.

One selects news items associated with a particular category. When a user filters by category, I also like news articles that have been tagged with a different category to be displayed.

So, I have another query that returns news objects tagged with a specific category.

Now I would like to combine the two collections by removing duplicates (as a news article related to the main category, you can also mark the second category).

 var catNews = model.Category.News.SelectMany(n => n.News); //get news article associated to the category
 var tagNews = _nr.GetNews(model.Category.relatedCategoryName); //this selects news by tags - which I want as the related category name
 model.News = catNews.Union(tagNews).OrderByDescending(p => p.Date); //union the two collections

However, model.News now contains two identical news articles, and I'm not sure why, because should the union use the default equalizer?

Am I doing something wrong here? I use EF Code First, and my primary key is the news identifier.

The way I ran into this problem is to pass the list of catNews id to GetNews and exclude them

if (excludeIds != null)
    q = q.Where(n => !excludeIds.Contains(n.ID));

But I'm not sure why I get it when I thought that the union would delete identical articles?

+3
source share
1 answer

, . , , News , Id , , News Union , Concat. Equals ( GetHaschCode) News Id .

+3

All Articles