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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:43:08+00:00 2026-06-02T01:43:08+00:00

Which is the easiest way to remove items that match some condition from a

  • 0

Which is the easiest way to remove items that match some condition from a list and then, get those items.

I can think in a few ways, I don’t know which is the best one:

var subList = list.Where(x => x.Condition);
list.RemoveAll(x => x.Condition);

or

var subList = list.Where(x => x.Condition);
list.RemoveAll(x => subList.Contains(x));

Is any of this one of the best ways? If it is, which one? If it’s not, how should I do it?

  • 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-02T01:43:10+00:00Added an answer on June 2, 2026 at 1:43 am

    I would go with the first option for readability purposes, with the note that you should materialize the list first, or you’ll lose the very items you’re trying to select on the next line:

    var sublist = list.Where(x => x.Condition).ToArray();
    list.RemoveAll(x => x.Condition);
    

    The second example is O(n^2) for no reason and the last is perfectly fine, but less readable.

    Edit: now that I reread your last example, note that as it’s written right now will take out every other item. You’re missing the condition check and the remove line should actually be list.RemoveAt(i--); because the i+1th element becomes the ith element after the removal, and when you increment i you’re skipping over it.

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

Sidebar

Related Questions

Which is the easiest way to get all form elements which are contained by
I'm getting data from a stored procedure and wonder which is the easiest way
Which is the easiest way I could get the file details / properties under
Hi I have a quick question, which is the easiest way to loop inside
What is the easiest way to create a Silverlight Button with custom Content which
What's the easiest way to convert a C++ std::string to another std::string, which has
Using Paypal, what is the easiest way to create a page on which a
Which is the easiest and most unobstrusive way to keep an ASP.NET session alive
Whats the easiest way to build a simple 'web' application which is a single
Sorry for my beginner's question... What is the easiest way to define procedures, 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.