Deleting items from the list using LINQ


This question already has an answer here:

  • Using LINQ to remove elements from a List<T> 14 answers

I have a List of type some entity


public class OrderLine
   public string productCode;
   public int quantity;


i need to remove items from the above List if the productCode is equal to some products.

List<string> ProductsToBeExcluded = new List<string>(){"1234","1237"};

so, from List<OrderLine> i need to remove products which are equal to 1234 and 1237

i have tried

  1. create a List<string> from List<OrderLine> using

      List<OrderLine> OrderLines = GetOrderLines();
      var ol = from o in OrderLines
            select o.ProductCode;


  List<string> ProductsToBeExcluded = new List<string>(){"1234","1237"};
   var filtered = OrderLines.Except(ProductsToBeExcluded);

how do I proceed further in removing


In this case you don't need LINQ but can just use List<T>.RemoveAll instead

OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));