.Contains()在自定义类对象列表上

时间:2010-04-13 11:34:42

标签: c# list class contains

我正在尝试在自定义对象列表中使用.Contains()函数

这是清单:

List<CartProduct> CartProducts = new List<CartProduct>();

CartProduct

public class CartProduct
{
    public Int32 ID;
    public String Name;
    public Int32 Number;
    public Decimal CurrentPrice;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ID">The ID of the product</param>
    /// <param name="Name">The name of the product</param>
    /// <param name="Number">The total number of that product</param>
    /// <param name="CurrentPrice">The currentprice for the product (1 piece)</param>
    public CartProduct(Int32 ID, String Name, Int32 Number, Decimal CurrentPrice)
    {
        this.ID = ID;
        this.Name = Name;
        this.Number = Number;
        this.CurrentPrice = CurrentPrice;
    }
    public String ToString()
    {
        return Name;
    }
}

所以我尝试在列表中找到类似的cartproduct:

if (CartProducts.Contains(p))

但它忽略了类似的购物车产品,我似乎不知道它检查了什么 - ID?还是全部?

提前致谢! :)

7 个答案:

答案 0 :(得分:120)

如果您使用的是.NET 3.5或更高版本,则可以使用LINQ扩展方法通过Any扩展方法实现“包含”检查:

if(CartProducts.Any(prod => prod.ID == p.ID))

这将检查CartProducts中是否存在ID与p ID匹配的产品。您可以在=>之后放置任何布尔表达式来执行检查。

这也有利于LINQ-to-SQL查询以及内存查询,Contains没有。

答案 1 :(得分:103)

您需要实施IEquatable或覆盖Equals()GetHashCode()

例如:

public class CartProduct : IEquatable<CartProduct>
{
    public Int32 ID;
    public String Name;
    public Int32 Number;
    public Decimal CurrentPrice;

    public CartProduct(Int32 ID, String Name, Int32 Number, Decimal CurrentPrice)
    {
        this.ID = ID;
        this.Name = Name;
        this.Number = Number;
        this.CurrentPrice = CurrentPrice;
    }

    public String ToString()
    {
        return Name;
    }

    public bool Equals( CartProduct other )
    {
        // Would still want to check for null etc. first.
        return this.ID == other.ID && 
               this.Name == other.Name && 
               this.Number == other.Number && 
               this.CurrentPrice == other.CurrentPrice;
    }
}

答案 2 :(得分:10)

检查列表中是否包含特定对象。

您最好使用列表中的Find方法。

这是一个例子

List<CartProduct> lst = new List<CartProduct>();

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

希望有所帮助

你也应该看看LinQ - 这可能是一种过度杀伤,但仍然是一个有用的工具......

答案 3 :(得分:4)

默认情况下,引用类型具有引用相等性(即,如果它们是同一个对象,则两个实例仅相等)。

您需要覆盖Object.Equals(和Object.GetHashCode匹配)以实现您自己的平等。 (然后,优良作法是实现相等,==,运算符。)

答案 4 :(得分:1)

您需要从列表中创建一个对象,例如:

List<CartProduct> lst = new List<CartProduct>();

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

该对象通过其属性x.name

来获得外观值

然后,您可以使用包含或删除之类的列表方法

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

答案 5 :(得分:0)

如果你想控制它,你需要实现[IEquatable接口] [1]

[1]:http://This方法通过使用默认的相等比较器来确定相等性,由对象的T的IEquatable.Equals方法的实现(列表中的值的类型)定义。

答案 6 :(得分:0)

实施override Equals()GetHashCode()

public class CartProduct
{
    public Int32 ID;
    ...

    public CartProduct(Int32 ID, ...)
    {
        this.ID = ID;
        ...
    }

    public override int GetHashCode()
    {
        return ID;
    }

    public override bool Equals(Object obj)
        {
            if (obj == null || !(obj is CartProduct))
                return false;
            else
                return GetHashCode() == ((CartProduct)obj).GetHashCode();
        }

}

已使用:

if (CartProducts.Contains(p))