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);
var tagNews = _nr.GetNews(model.Category.relatedCategoryName);
model.News = catNews.Union(tagNews).OrderByDescending(p => p.Date);
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?
source
share