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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:32:25+00:00 2026-05-26T21:32:25+00:00

From the previous question I asked, RemoveAll is the cleanest way to remove from

  • 0

From the previous question I asked, RemoveAll is the cleanest way to remove from a List<> based on a condition. Curious to know what is the best way to remove from a LinkedList as there is no RemoveAll function there.

List<ItemClass> itemsToErase = new List<ItemClass>();
    foreach(ItemClass itm in DS)
    {
           if(itm.ToBeRemoved)
                itemsToErase .Add(itm);
    }
    foreach(ItemClass eraseItem in itemsToErase)
    {
          DS.Remove(eraseItem );
    }          

EDIT: DS is of type LinkedList<ItemClass>

  • 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-26T21:32:26+00:00Added an answer on May 26, 2026 at 9:32 pm

    While you can’t remove nodes from a LinkedList<T> while iterating it with foreach, you can manually iterate the LinkedList<T> by following the Next property of each LinkedListNode<T>. Just remember the next node of the node before removing it:

    var list = new LinkedList<int>(Enumerable.Range(0, 10));
    var node = list.First;
    while (node != null)
    {
        var next = node.Next;
        if (node.Value % 2 == 0)
            list.Remove(node);
        node = next;
    }
    

    Extension Method:

    public static int RemoveAll<T>(this LinkedList<T> list, Predicate<T> match)
    {
        if (list == null)
        {
            throw new ArgumentNullException("list");
        }
        if (match == null)
        {
            throw new ArgumentNullException("match");
        }
        var count = 0;
        var node = list.First;
        while (node != null)
        {
            var next = node.Next;
            if (match(node.Value))
            {
                list.Remove(node);
                count++;
            }
            node = next;
        }
        return count;
    }
    

    Usage:

    LinkedList<ItemClass> DS = ...
    DS.RemoveAll(itm => itm.ToBeRemoved);
    

    See also: Extension Methods (C# Programming Guide)

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

Sidebar

Related Questions

based my previous question asked here: Jquery Filter List DIVs via checkboxes i got
This stems from a previous question I asked - about a write conflict with
This is a continuation question from a previous question I have asked I now
#&q=car&category=Car%20Audio%2CAccessories&brand= I borrowed a this function from a previous question asked on SO: function
From the previous question I asked, I am still having the same question. I
Following on from a previous question in which I asked: How can I use
This stems from a previous question I asked regarding code metrics. I have been
This is a separate problem lingering from a previous question I asked here on
The previous question I asked about creating a DIV where size is calculated from
This follows on from a previous question here where I asked how to develop

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.