Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If Lines is a <code>List&lt;T&gt;</code>, then the simplest way is to just write:</p> <pre><code>cart.Lines.RemoveAll(x =&gt; x.Quantity == 0); </code></pre> <p>If Lines is an <code>IEnumerable&lt;T&gt;</code>, though, you could select the negative (as Vlad suggested) - you could also change to a list using <code>ToList()</code> and then do <code>RemoveAll()</code> but that would be overkill.</p> <pre><code>cart.Lines = cart.Lines.Where(x =&gt; x.Quantity != 0); </code></pre> <p><strong>UPDATE:</strong></p> <p>Since you said Lines is an <code>IList&lt;T&gt;</code>, then you will want to select the negative and convert to a list like:</p> <pre><code>cart.Lines = cart.Lines.Where(x =&gt; x.Quantity != 0).ToList(); </code></pre> <p>Or you can conver to a <code>List&lt;T&gt;</code> using <code>ToList()</code> then call <code>RemoveAll()</code> and then save back:</p> <pre><code>var temp = cart.Lines.ToList(); temp.RemoveAll(x =&gt; x.Quantity != 0); cart.Lines = temp; </code></pre> <p>Incidentally, as an FYI I timed both building a remove list and then using <code>Remove()</code> vs selecting the negative using <code>Where()</code> and calling <code>ToList()</code> 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. </p> <p>Here's the timing for removing all the even numbers out of a list of 100,000 ints:</p> <ul> <li>Removing all evens building a remove list and calling Remove() on each took: 3921 ms</li> <li>Removing all evens using Where() on negative and then ToList() took: 2 ms</li> <li>Removing all evens using ToList() on original then RemoveAll() took: 1 ms</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload