List.Contains is not working as hoped

前端 未结 4 1142
[愿得一人]
[愿得一人] 2020-12-06 08:28

If I have an object of type MyBull and a List orig:

// Just an example
MyBull x = getMeTheObjectWithIdFromDB(9);

ori         


        
4条回答
  •  天涯浪人
    2020-12-06 09:12

    If you can use LINQ then you can

    class Vessel
    {
        public int id { get; set; }
        public string name { get; set; }
    }
    

    ...

    var vessels = new List() { new Vessel() { id = 4711, name = "Millennium Falcon" } };
    
    var ship = new Vessel { id = 4711, name = "Millencolin" };
    
    if (vessels.Any(vessel => vessel.id == ship.id))
        Console.Write("There can be only one!");
    

提交回复
热议问题