想要在列表中添加唯一数组<>

时间:2013-08-07 05:06:54

标签: c# list

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

但根据我的要求,它无法正常工作。

------全球一流-----

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

-----定义列表globaly -----

      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);
                        }                            
                    }
                }

2 个答案:

答案 0 :(得分:3)

您需要覆盖Equals(object)课程上的filesdetail方法,Contains可以使用该方法检查两个filesdetail是否匹配。

如果对象完全是同一个对象,则默认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;
    }
}

答案 1 :(得分:0)

您好,您可以尝试Distinct方法

    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; }
}