0

我想在列表中添加唯一数组。下面是我到目前为止尝试过的代码。

但它没有按照我的要求工作。

------全局类--

    public class filesdetail
        {
            public string truckno { get; set; }
            public string deliveryno { get; set; }
        }

-----全局定义列表-----

      public List<filesdetail> arrr_lst_truck_del;

--------事件中的代码-------

      arrr_lst_truck_del = new List<filesdetail>();
      for (int i = 0; i < temp.Length; i++)
                {

                    if (temp[i].Split(Convert.ToChar(9)).Length > 1)
                    { 
                        obj = new filesdetail();
                        obj.truckno = temo1;
                        obj.deliveryno = temp[i].Split(Convert.ToChar(9))[0].Trim('\n');
                        if (!arrr_lst_truck_del.Contains(obj))
                        {
                            arrr_lst_truck_del.Add(obj);
                        }                            
                    }
                }
4

2 回答 2

3

您需要覆盖类Equals(object)上的方法,然后可以使用该方法检查两个s 是否匹配。filesdetailContainsfilesdetail

只有当对象是完全相同的对象时,默认Equals方法才会返回,所以如果你这样做:true

new filesdetail().Equals(new filesdetail())

......这永远不会是真的。

这是一个示例实现。实施IEquatable<T>以表明您有一个健全的平等方法是一个很好的做法。我还实施GetHashCode了很好的措施:

public class filesdetail : IEquatable<filesdetail>
{
    public string truckno { get; set; }
    public string deliveryno { get; set; }

    public bool Equals(filesdetail other)
    {
        return other != null &&
                Equals(truckno, other.truckno) &&
                Equals(deliveryno, other.deliveryno);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as filesdetail);
    }

    public override int GetHashCode()
    {
        var trucknoHash = truckno == null ? 0 : truckno.GetHashCode();
        var deliverynoHash = deliveryno == null ? 0 : deliveryno.GetHashCode();

        return (trucknoHash*397) ^ deliverynoHash;
    }
}
于 2013-08-07T05:13:33.577 回答
0

嗨,你可以尝试不同的方法

    class Program
{
    static void Main(string[] args)
    {
        List<filesdetail> details = new List<filesdetail>();
        details.Add(new filesdetail() { truckno = "123", deliveryno = "abc" });
        details.Add(new filesdetail() { truckno = "123", deliveryno = "abc" });
        var a = details.Distinct(new filesdetailComparer<filesdetail>()).ToList();
    }
}

public class filesdetailComparer<T> : IEqualityComparer<T>
{
    public bool Equals(T x, T y)
    {
        var obj1 = x as filesdetail;
        var obj2 = y as filesdetail;

        if (obj1 != null && obj2 != null && string.Compare(obj1.deliveryno, obj2.deliveryno) == 0 && string.Compare(obj1.truckno, obj2.truckno) == 0)
            return true;
        else
            return false;
    }

    public int GetHashCode(T obj)
    {
        var obj1 = obj as filesdetail;
        //Check whether the object is null 
        if (Object.ReferenceEquals(obj1, null)) return 0;

        //Get hash code for the truckno field if it is not null. 
        int hashtruckno = obj1.truckno == null ? 0 : obj1.deliveryno.GetHashCode();

        //Get hash code for the deliveryno field. 
        int hashdeliveryno = obj1.deliveryno == null ? 0 : obj1.deliveryno.GetHashCode();

        //Calculate the hash deliveryno for the filesdetail. 
        return hashtruckno ^ hashdeliveryno;
    }
}

public class filesdetail
{
    public string truckno { get; set; }
    public string deliveryno { get; set; }
}
于 2013-08-07T05:43:03.963 回答