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 3339924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:32:55+00:00 2026-05-18T00:32:55+00:00

I have two collections of objects of different type. Lets call them type ALPHA

  • 0

I have two collections of objects of different type. Lets call them type ALPHA and type BRAVO. Each of these types has a property that is the “ID” for the object. No ID is duplicated within the class, so for any given ID, there is at most one ALPHA and one BRAVO instance. What I need to do is divide them into 3 categories:

  1. Instances of the ID in ALPHA which do not appear in the BRAVO collection;
  2. Instances of the ID in BRAVO which do not appear in the ALPHA collection;
  3. Instances of the ID which appear in both collections.

In all 3 cases, I need to have the actual objects from the collections at hand for subsequent manipulation.

I know for the #3 case, I can do something like:

 var myCorrelatedItems = myAlphaItems.Join(myBravoItems, alpha => alpha.Id, beta => beta.Id, (inner, outer) => new
            {
                alpha = inner,
                beta = outer
            });

I can also write code for the #1 and #2 cases which look something like

var myUnmatchedAlphas = myAlphaItems.Where(alpha=>!myBravoItems.Any(bravo=>alpha.Id==bravo.Id));

And similarly for unMatchedBravos. Unfortunately, this would result in iterating the collection of alphas (which may be very large!) many times, and the collection of bravos (which may also be very large!) many times as well.

Is there any way to unify these query concepts so as to minimize iteration over the lists? These collections can have thousands of items.

  • 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-18T00:32:55+00:00Added an answer on May 18, 2026 at 12:32 am

    If you are only interested in the IDs,

    var alphaIds = myAlphaItems.Select(alpha => alpha.ID);
    var bravoIds = myBravoItems.Select(bravo => bravo.ID);
    
    var alphaIdsNotInBravo = alphaIds.Except(bravoIds);
    var bravoIdsNotInAlpha = bravoIds.Except(alphaIds);
    

    If you want the alphas and bravos themselves,

    var alphaIdsSet = new HashSet<int>(alphaIds);
    var bravoIdsSet = new HashSet<int>(bravoIds);
    
    var alphasNotInBravo = myAlphaItems
                           .Where(alpha => !bravoIdsSet.Contains(alpha.ID));
    
    var bravosNotInAlpha = myBravoItems
                           .Where(bravo => !alphaIdsSet.Contains(bravo.ID));
    

    EDIT:
    A few other options:

    1. The ExceptBy method from MoreLinq.
    2. The Enumerable.ToDictionary method.
    3. If both types inherit from a common type (e.g. an IHasId interface), you could write your own IEqualityComparer<T> implementation; Enumerable.Except has an overload that accepts an equality-comparer as a parameter.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I use unbounded wildcard types for two collections (each collection will have a
I have a collection of objects of the same type, let's call it DataItem
I have an ArrayList of objects that I need to sort in two different
I have two different NSDictionary objects where there are keys that belong to both
I have two collections which have property Email in both collections. I need to
I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Suppose I have Two Objects: object1, object2 When I do the following assign: object2.Foo
I have a List<> of objects containing two strings and a DateTime. I want
I have two objects... and if I compile a program with either one, it

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.