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
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
In this case you don't need LINQ but can just use List<T>.RemoveAll instead
OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
Use RemoveAll method of List which accepts predicate
OrderLines.RemoveAll(x => ProductsToBeExcluded.Contains(x.ProductCode));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With