Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda usage is confusing me

Tags:

c#

asp.net

lambda

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.

like image 790
Jordan Avatar asked Oct 22 '25 15:10

Jordan


2 Answers

They are a shorthand for a delegate. In your case, the same code without lambdas would look like this:

    public void RemoveLine( Product product )
    {
        var helper = new RemoveAllHelper();
        helper.product = product;

        lineCollection.RemoveAll( helper.TheMethod );
    }

    class RemoveAllHelper
    {
        public Product product;
        public bool TheMethod( CartLine p ) { return p.Product.ProductID == product.ProductID; }
    }

Because your lambda contains a variable that is defined outside of the lambda (called "bound" or "captured" variable), the compiler has to create a helper object with a field to put that variable into.

For lambdas without bound variables, a static method may be used:

    public void RemoveLine( Product product )
    {
        lineCollection.RemoveAll( TheMethod );
    }

    public static bool TheMethod( CartLine p ) { return p.Product.ProductID == 5; }

When the only bound variable is this, then an instance method on the same object may be utilized:

    public void RemoveLine( Product product )
    {
        lineCollection.RemoveAll( this.TheMethod );
    }

    public bool TheMethod( CartLine p ) { return p.Product.ProductID == this.targetProductID; }
like image 58
Fyodor Soikin Avatar answered Oct 24 '25 04:10

Fyodor Soikin


RemoveAll implementation has some sort of iterator which calls your anonymous function for each iteration. Like:

iterator {
    your_lambda_function(current_item);
}

And p => p.Product.ProductID == product.ProductID can be rewritten as:

delegate(CartLine p) { return p.Product.ProductID == product.ProductID; }

which could look a bit clearer for you

like image 39
zerkms Avatar answered Oct 24 '25 03:10

zerkms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!