Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5952933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:45:07+00:00 2026-05-22T17:45:07+00:00

I have two IEnumerable collections that I would like to union. One selects news

  • 0

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

One selects news objects that are associated with a particular category. When a user is filtering by a category I would also like news articles that have been tagged with another category to be displayed.

So I have another query that returns news objects that are tagged with the particular sub category.

Now I would like to union the two collections, removing duplicates (as a news article associated to the main category, may also be tagged with 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 am not sure why as union should use the default equality comparer?

Am I doing something wrong here? I am using EF Code First and my primary key is the news id.

The way I have got round this issue is by passing in a list of the catNews id to the GetNews function and excluding them

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

But I am not sure why I have to this when I thought union would remove the identical articles?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:45:08+00:00Added an answer on May 22, 2026 at 5:45 pm

    I guess that you are not loading these two collections from the same instance of the entity framework context. Default equality comparer will compare references and if you use the same context it will indeed return same News instance in both collections when Id is matching but if you use different contexts each collection will contain its own News instances and Union will do the same as Concat. In such case you must override Equals (and GetHaschCode) in your News entity to compare Id or use custom comparer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two enumerables that I would like to use with PLINQ, how do
I have two different collections: List<int> IEnumerable<IDataRecord> The first contains a list of primary
Let's say you have two collections of integers: IEnumerable<int> col1=new List<int> {2,3,3,5,7,11,11,11,13}; IEnumerable<int> col2=new
I have two classes.... Parcel and FundParcel...and I'm trying to convert an IEnumerable of
I have a IEnumerable. I have a custom Interval class which just has two
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two classes (MVC view model) which inherits from one abstract base class.
I have a worker class that does stuff with a collection of objects. I
I have an object that holds an object that is definded as IEnumerable, i.e.
I have a IEnumerable collection of objects. The objects have a field LastedUpdated which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.