3

I have a List of type some entity

List

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;
    

2.

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

how do I proceed further in removing

thanks

Purushotham
  • 3,770
  • 29
  • 41
CSharped
  • 1,247
  • 4
  • 20
  • 49

2 Answers2

8

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

OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

Use RemoveAll method of List which accepts predicate

OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105