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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:48:27+00:00 2026-05-23T17:48:27+00:00

I have a cart.Lines List and want to remove all items where quantity ==

  • 0

I have a cart.Lines List and want to remove all items where quantity == 0

This is a list that holds collection of CartLine objects:

public class Cart
{
    private IList<CartLine> lines = new List<CartLine>();
    public IList<CartLine> Lines { get { return lines; } set { lines = value; } }
}   

public class CartLine
{
    Product Product {get; set;}
    int Quantity {get; set;}

}

So something like:

cart.Lines.RemoveAll(x => x.Quantity == 0)

I only get Remove and RemoveAt, not RemoveAll !

Also can’t remove in a foreach loop, get error:
Collection was modified; enumeration operation may not execute.

I have now managed to do it with this code, surely there must be something more efficient ?

var myList = cart.Lines.ToList();
myList.RemoveAll(x => x.Quantity == 0);
cart.Lines = myList;

Okay Problem solved!Thanks guys, this here does it:

cart.Lines = cart.Lines.Where(x => x.Quantity != 0);

  • 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-23T17:48:28+00:00Added an answer on May 23, 2026 at 5:48 pm

    If Lines is a List<T>, then the simplest way is to just write:

    cart.Lines.RemoveAll(x => x.Quantity == 0);
    

    If Lines is an IEnumerable<T>, though, you could select the negative (as Vlad suggested) – you could also change to a list using ToList() and then do RemoveAll() but that would be overkill.

    cart.Lines = cart.Lines.Where(x => x.Quantity != 0);
    

    UPDATE:

    Since you said Lines is an IList<T>, then you will want to select the negative and convert to a list like:

    cart.Lines = cart.Lines.Where(x => x.Quantity != 0).ToList();
    

    Or you can conver to a List<T> using ToList() then call RemoveAll() and then save back:

    var temp = cart.Lines.ToList();
    temp.RemoveAll(x => x.Quantity != 0);
    cart.Lines = temp;
    

    Incidentally, as an FYI I timed both building a remove list and then using Remove() vs selecting the negative using Where() and calling ToList() and the Where/ToList combo was much faster which makes sense because both allocate memory, but the Where/ToList does a lot less memory shuffling.

    Here’s the timing for removing all the even numbers out of a list of 100,000 ints:

    • Removing all evens building a remove list and calling Remove() on each took: 3921 ms
    • Removing all evens using Where() on negative and then ToList() took: 2 ms
    • Removing all evens using ToList() on original then RemoveAll() took: 1 ms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple cart page that displays items that are in someones cart,
I have function getCartItems in cart.js and I want to call that function in
i have a variable $cartTotal in cart.php page i want to use that same
I have a cart table which has a relation to a product table that
I have an Action that basically adds an item to a cart, the only
I am doing a shopping cart tutorial: I have an array that collects input
We have noticed that the shopping cart banners seem to frequently loose the link
I'm building an ASP.Net website. I have a cart class which stores the items
I'm using Magento 1.4 and I want to remove the Checkout and My Cart
I have this line of code: <%= link_to Add to cart, :controller => car,

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.