.Contains() on a list of custom class objects

前端 未结 7 2176
天命终不由人
天命终不由人 2020-11-27 02:38

I\'m trying to use the .Contains() function on a list of custom objects

This is the list:

List CartProducts = new Lis         


        
7条回答
  •  执笔经年
    2020-11-27 03:21

    You need to create a object from your list like:

    List lst = new List();
    
    CartProduct obj = lst.Find(x => (x.Name == "product name"));
    

    That object get the looked value searching by their properties: x.name

    Then you can use List methods like Contains or Remove

    if (lst.Contains(obj))
    {
       lst.Remove(obj);
    }
    

提交回复
热议问题