troubleshooting Question

Lamda expression - Can you explain this to me...?

Avatar of jparlato
jparlatoFlag for United States of America asked on
.NET ProgrammingC#
4 Comments1 Solution387 ViewsLast Modified:
      In the code below, I'm just having trouble understanding the x => x.
       What is the x and where does it come from... I understand that the IEnumerable list, lines, is being used in the linq function FirstOrDefault to find a product with the id specified in the pam input.  I just want to better understand why and what the  x => x. . is and why it is necessary in the syntax.
It seems to me, if it weren't their, the same interpretation could be obtained of the code and the same processing could be generated... I understand the code, just not why this syntax requires the  x => x.
Can someone help me with this?
       

       private List<CartLine> lines = new List<CartLine>();
        public IList<CartLine> Lines { get { return lines.AsReadOnly(); } }

        public void AddItem(Product product, int quantity)
        {
            var line = lines.FirstOrDefault(x => x.Product.ProductID == product.ProductID);
            if (line == null)
                lines.Add(new CartLine { Product = product, Quantity = quantity });
            else
                line.Quantity += quantity;
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros