So I am implementing a project from a book and I am somewhat confused as to WHY i need these lambdas.
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
public class CartLine
{
public Product Product { get; set; }
public int Quantity { get; set; }
}
public void RemoveLine(Product product)
{
lineCollection
.RemoveAll(p => p.Product.ProductID == product.ProductID);
}
}
Why do I need .RemoveAll(p=> p.Product.ProductID == product.ProductID)?
Does it just have to do with .RemoveAll NEEDING a lambda expression? I’m not sure why I can’t use this.Product.ProductID, I realize Product is a list, is p=> P.Product doing some sort of iteration and comparing the values till it finds what it needs?
Product is defined in
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
I’m generally confused as to the purpose of Lambdas in these kinds of things. Is there some equivelant to p=>p.Product.ProductID == product.ProductID
I could see not using Lambdas so I could understand more what it is shorthand for??
I can’t seem to wrap my head around this one, and thanks in advance.
RemoveAllimplementation has some sort of iterator which calls your anonymous function for each iteration. Like:And
p => p.Product.ProductID == product.ProductIDcan be rewritten as:which could look a bit clearer for you