.Contains() on a list of custom class objects

前端 未结 7 2206
天命终不由人
天命终不由人 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:38

    It checks to see whether the specific object is contained in the list.

    You might be better using the Find method on the list.

    Here's an example

    List lst = new List();
    
    CartProduct objBeer;
    objBeer = lst.Find(x => (x.Name == "Beer"));
    

    Hope that helps

    You should also look at LinQ - overkill for this perhaps, but a useful tool nonetheless...

提交回复
热议问题