I have following classes
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public int SectionId { get; set; }
public string VendorName { get; set; }
}
public class ProductToRemove
{
public int CategoryId { get; set; }
public int SectionId { get; set; }
}
In the main I have list of these two classes as follows.
List<Product> Products = new List<Product>()
{
new Product() { Id = 1, Name = "A", CategoryId = 11, SectionId = 6, VendorName = "ABC" },
new Product() { Id = 2, Name = "B", CategoryId = 21, SectionId = 6, VendorName = "ABC" },
new Product() { Id = 3, Name = "C", CategoryId = 13, SectionId = 8, VendorName = "ABC" },
new Product() { Id = 4, Name = "D", CategoryId = 90, SectionId = 6, VendorName = "ABC" },
new Product() { Id = 5, Name = "E", CategoryId = 25, SectionId = 9, VendorName = "ABC" },
};
List<ProductToRemove> ProductsToRemove = new List<ProductToRemove>()
{
new ProductToRemove() {CategoryId = 11, SectionId = 6, },
new ProductToRemove() {CategoryId = 90, SectionId = 6, }
};
I want to remove anything from Products instance where CategoryId and SectionId matches what is in the ProductsToRemove collection. I know how to loop through Products collection and delete matching records but I am wondering if there is a way to do the same using Linq