I want to find and return the first CartLine
that has a match of product in a Cart
with the FindProductInCartLines(int productId)
method. If occurrence can't be found return null
.
However, the compiler throws an error
Cannot implicitly convert type: CartLine to Product
with the FirstOrDefault()
line number.
I'm a real newbie concerning lambda functions and delegates. I thought it was already dereferenced with line.Product.Id
but obviously I'm having some kind of misunderstanding.
I tried using JaredPar's answer from: Create IEnumerable<T>.Find() But I don't see the difference between my code and his.
public class CartLine
{
public int OrderLineId { get; set; }
public Product Product { get; set; }
public int Quantity { get; set; }
}
public IEnumerable<CartLine> Lines => GetCartLineList();
private List<CartLine> GetCartLineList()
{
return new List<CartLine>();
}
public Product FindProductInCartLines(int productId)
{
return Lines.FirstOrDefault(line => line.Product.Id == productId);
}