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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:03:00+00:00 2026-06-16T23:03:00+00:00

I have two lists. I want to remove any items from LIST1 that are

  • 0

I have two lists. I want to remove any items from LIST1 that are NOT present in LIST2.

So for example:

        var list1 = new List<DownloadTask>();
        list1.Add(new DownloadTask{ OperationID = 1, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 2, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 3, MachineID = 1 });
        list1.Add(new DownloadTask{ OperationID = 3, MachineID = 2 });

        var list2 = new List<DownloadTask>();
        list2.Add(new DownloadTask{ OperationID = 1, MachineID = 1 });
        list2.Add(new DownloadTask{ OperationID = 3, MachineID = 2 });

After run list1 should contain only items: with combination operationId = 1, machineId = 1 AND OperationId = 3, MachineId =2.

  • 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-16T23:03:01+00:00Added an answer on June 16, 2026 at 11:03 pm

    Does DownloadTask override Equals and GetHashCode correctly? If so, all you need is:

    list1 = list1.Intersect(list2).ToList();
    

    That’s if you’re happy to create a new list, of course. If you really want to remove them from the existing list, it’s slightly harder. It would quite possibly be simplest to work out what the result should look like, then clear and re-add:

    var newList = list1.Intersect(list2).ToList();
    list1.Clear();
    list1.AddRange(newList);
    

    Of course, all of this does require you to implement equality appropriately in DownloadTask – but if you haven’t done so already, it sounds like it would be a good idea to do so. (Or at least implement IEqualityComparer<DownloadTask> somewhere – you can pass a comparer to Intersect.)

    As a side note, I view “only keep the elements in list1 which are also in list2” (i.e. intersection) as a simpler way of looking at the problem than “remove all elements from list1 which aren’t in list2” – the latter is basically a double negative, which is always a bit of a pain.

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

Sidebar

Related Questions

I have two employee lists that I want to get only unique records from
So I have two lists and I want to use .pop() to remove an
I have two lists which are guaranteed to be the same length. I want
I have two lists: link_ids = ['111','222','333'] filenames = ['111-foo.txt','111-bar.txt','222.txt'] I want to do
I have two lists: [a, b, c] [d, e, f] I want: [a, d,
If I have two generic lists, List, and I want to merge all the
I have two tables with list of urls fetched from different sources. I want
I have two Lists of dictionaries. Both are not not null. How to merge
I'm having two sortable lists where I can add items from the first list
Imagine if there are two lists and the two lists have some similar items.

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.