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

  • Home
  • SEARCH
  • 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 8279695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:24:42+00:00 2026-06-08T09:24:42+00:00

I have two collections. var a = new List<string>() { a, b, c, d,

  • 0

I have two collections.

  var a = new List<string>() { "a", "b", "c", "d", "e", "f", "j" };
  var b = new List<string>() { "a", "c", "d", "h", "i" };

And I would like to do some action on the item in case it’s missing in one collection or another.

public static Synchronize<T>(IEnumerable<T> first, IEnumerable<T> second, Action<T> firstSynchronizer, Action<T> secondSynchronizer)
{
  var firstUnique = first.Distinct();
  var secondUnique = second.Distinct();
  foreach (var item in firstUnique)
  {
    if (!secondUnique.Contains(item)) firstSynchronizer(item);
  }
  foreach (var item in second.Distinct())
  {
    if (!firstUnique.Contains(item)) secondSynchronizer(item);
  }
}

This is what I got but I am not happy with it. I can’t help but wonder if there’s a better way to implement this, because I think Distinct() is pretty big performance hit and also I am not sure if it’s better to iterate whole second Enumerable and check if item is not present in first Enumerable already (like above) or if it would be better to iterate second.Except(first)? What do you guys think?

I call it like this:

  var a = new List<string>() { "a", "b", "c", "d", "e", "f", "j" };
  var b = new List<string>() { "a", "c", "d", "h", "i" };
  Synchronize(a.ToArray(), b.ToArray(), t => b.Add(t), t => a.Add(t));

I call ToArray() so collections don’t get changed while being iterated over and lambdas just add missing elements to respective lists.

Also, this is just a test implementation. In production environment, Enumerables won’t be of same type. This is intended to be used to sync remote and local storage. In future, Enumerable first will be for example ICollection<DummyRemoteItem> and Enumerable second will be List<IO.FileSystemInfo>. But I want it to be more generic. To make it possible to work with different collections, I think I would propose another type parameter and a Func<T1, T2, bool> for comparing items. That would be a best approach, right?

Generally, what’s the best way to implement insides of

Synchronize<T>(IEnumerable<T> first,IEnumerable<T> second,Action<T> firstSynchronizer,Action<T> secondSynchronizer) 

and

Synchronize<TFirst, TSecond>(IEnumerable<TFirst> first,IEnumerable<TSecond> second,Action<TFirst> firstSynchronizer,Action<TSecond> secondSynchronizer, Func<TFirst, TSecond, bool> predicate)
  • 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-06-08T09:24:43+00:00Added an answer on June 8, 2026 at 9:24 am

    Linq full outer join is your friend here.

    Here’s an implementation (from here)

    public static IEnumerable<Tuple<T1, T2>> FullOuterJoin<T1, T2>
       (this IEnumerable<T1> one, IEnumerable<T2> two, Func<T1,T2,bool> match)
    {
     var left = from a in one
       from b in two.Where((b) => match(a, b)).DefaultIfEmpty()
       select new Tuple<T1, T2>(a, b);
    
     var right = from b in two
       from a in one.Where((a) => match(a, b)).DefaultIfEmpty()
       select new Tuple<T1, T2>(a, b);
    
     return left.Concat(right).Distinct();
    }
    

    so:

    a.FullOuterJoin(b,a=>a,b=>b,(a,b)=>new {a,b})
    

    and look for nulls in the resulting enumerable.

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

Sidebar

Related Questions

I have a class which has two HashSet<String> collections as private members. Other classes
I have two different collections: List<int> IEnumerable<IDataRecord> The first contains a list of primary
I have two collections, and need to create a new collection from the two
I have two collections as below which hold IDs for Students. The ids are
I have two generic collections in my code, EventableCollection and EventableSortedList - which are
I have two databases named: DB_A and DB_B . Each database has one collection
I am currently trying to combine two collections into one for binding to a
I have two classes like so: public class SentEmailAttachment : ISentEmailAttachment { public SentEmailAttachment();
I have two related objects: ProgramSession and ProgramTask, with a one-to-many relationship. A ProgramSession
A simplified scenario: I have a List<Foo> . Foo has two properties Description (

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.