0

I am trying to use Linq to search for a certain Product. How would I conduct this?

public class ShoppingCart : List<CartLine>
{
    // implement constructors you want available
    public ShoppingCart(){}

    public ShoppingCart( IEnumerable<CartLine> collection ) : base( collection ) {}

    public ShoppingCart( int capacity ) : base( capacity ) {}
}

public class CartLine
{
    public int CartLineId { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ImageLocation { get; set; }

    public int? ProductCategoryId { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
}

ShoppingCart shoppingCart = new ShoppingCart();

Linq Query Search: This keeps giving me errors, trying to use Resharper to get this in my repository pattern, testing

 shoppingCart.Find(p=>p.CartLine.Product.ProductName) = "SamsungTV"
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
TVLamp
  • 31
  • 1
  • 8
  • Hi! In first, the predicate that is used in linq expression is wrong: it must return a boolean result. I think you want to use `shoppingCart.Find(p=>p.CartLine.Product == "SamsungTV")`. But `Product` property of a `CartLine` instance is not string. See [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.find?view=netframework-4.7.2) to use the method properly. – Alexander Oct 14 '18 at 06:41
  • What are you trying to do? search for `Product` with name `"SamsungTV"`? – Guy Oct 14 '18 at 06:41
  • you got it I believe, shoppingCart.Find(p => p.Product.ProductName == "SamsungTV"); Thanks – TVLamp Oct 14 '18 at 06:45
  • 1
    This is not linq. The `Find` method belongs to the `List` class. Also, you should read [Eric Lippert's answer](https://stackoverflow.com/a/21694054/3094533) to Why not inherit from List? And another thing - you can simply use a `Dictionary` to store the cart, where the `int` stands for quantity, and remove the `CartLine` class. – Zohar Peled Oct 14 '18 at 06:47
  • so many good suggestions, I was reading from here, he has Cartlineid, but thanks for the input https://github.com/Apress/pro-asp.net-core-mvc-2/blob/master/10%20-%20SportsStore%20-%20Cart/SportsStore/SportsStore/Models/Cart.cs – TVLamp Oct 14 '18 at 06:51
  • @ZoharPeled, oh, yes, you are right! It's all in a hurry. – Alexander Oct 14 '18 at 06:54

3 Answers3

2

In the line shoppingCart.Find(p=>p.CartLine.Product.ProductName), p is already a CartLine. You also need to compare ProductName inside the query. Try

CartLine cartLine = shoppingCart.Find(p => p.Product.ProductName == "SamsungTV");
Guy
  • 46,488
  • 10
  • 44
  • 88
1

For LINQ can u use a "where" :

var result =  shoppingCart.Product.Where(x => x.ProductName == "SamsungTV").ToList();

now you will have as a list of all in "Result" only where product name is Samsung TV ,to do what you like with it.

0

If you want to find instance of CartLine that' product name is "SamsungTV" you could use the following expression:

CartLine cartLine = shoppingCart.Find(p => String.Equals(p.Product?.ProductName, "SamsungTV", StringComparison.OrdinalIgnoreCase));

It will find product using ordinal (binary) sort rules and ignoring the case of product name. If you want to use another string comparison rules, define it using StringComparison enumeration.

Alexander
  • 4,420
  • 7
  • 27
  • 42