Asp.net JSON列表不使用Distinct命令删除重复项

Asp.net JSON列表不使用Distinct命令删除重复项,asp.net,json,asp.net-mvc,linq,Asp.net,Json,Asp.net Mvc,Linq,目前,我正在查询一个返回JSON字符串的web服务 url = @"redacted url; returnValue = new WebClient().DownloadString(url); 我将返回结果放入模型类中定义的项目列表中。然后,我运行第二个JSON调用,用相同的搜索词搜索不同的字段 url2 = @"redacted url2; returnValue2 = new WebClient().DownloadString(url2); 然后,我创建列表并使用AddRange组

目前,我正在查询一个返回JSON字符串的web服务

url = @"redacted url;
returnValue = new WebClient().DownloadString(url);
我将返回结果放入模型类中定义的项目列表中。然后,我运行第二个JSON调用,用相同的搜索词搜索不同的字段

url2 = @"redacted url2;
returnValue2 = new WebClient().DownloadString(url2);
然后,我创建列表并使用AddRange组合列表

List<Order> shipments = JsonConvert.DeserializeObject<List<Order>>(returnValue);
List<Order> shipments2 = JsonConvert.DeserializeObject<List<Order>>(returnValue2);
shipments.AddRange(shipments2);
但出于某种原因,它仍在返回副本。 你知道我做错了什么吗?
提前感谢您的帮助

我最后用Shyju上面的评论纠正了它。 我将Distinct更改为以下内容

return View(shipments.OrderBy(x => x.dtDateReceived).Distinct(new OrderComparer()).ToList());
然后构建了以下比较函数

// Custom comparer for the Order class
    class OrderComparer : IEqualityComparer<Order>
    {
        // Orders are equal if their names and order numbers are equal.
        public bool Equals(Order x, Order y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the order's properties are equal.
            return x.sWorkOrderNumber == y.sWorkOrderNumber && x.sCustomerOrderName == y.sCustomerOrderName;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(Order order)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(order, null)) return 0;

            //Get hash code for the sCustomerOrderName field if it is not null.
            int hashOrderName = order.sCustomerOrderName == null ? 0 : order.sCustomerOrderName.GetHashCode();

            //Get hash code for the sWorkOrderNumber field.
            int hashOrderCode = order.sWorkOrderNumber.GetHashCode();

            //Calculate the hash code for the order.
            return hashOrderName ^ hashOrderCode;
        }
    }
//订单类的自定义比较器
类OrderComparer:IEqualityComparer
{
//如果订单名称和订单号相等,则订单相等。
公共布尔等于(x阶,y阶)
{
//检查比较对象是否引用相同的数据。
if(Object.ReferenceEquals(x,y))返回true;
//检查是否有任何比较对象为空。
if(Object.ReferenceEquals(x,null)| | Object.ReferenceEquals(y,null))
返回false;
//检查订单的属性是否相等。
返回x.sWorkOrderNumber==y.sWorkOrderNumber&&x.sCustomerOrderName==y.sCustomerOrderName;
}
//对于一对对象,If Equals()返回true
//然后GetHashCode()必须为这些对象返回相同的值。
public int GetHashCode(订单)
{
//检查对象是否为空
if(Object.ReferenceEquals(order,null))返回0;
//如果sCustomerOrderName字段不为null,则获取其哈希代码。
int hashOrderName=order.sCustomerOrderName==null?0:order.sCustomerOrderName.GetHashCode();
//获取sWorkOrderNumber字段的哈希代码。
int hashOrderCode=order.sWorkOrderNumber.GetHashCode();
//计算订单的哈希代码。
返回hashOrderName^hashOrderCode;
}
}

订单
有许多属性。当一个对象与另一个对象进行比较时,是什么使它独一无二?您需要覆盖
Equals
GetHashCode
以比较OrderId值。看到这个谢谢你给我指出了正确的方向!
// Custom comparer for the Order class
    class OrderComparer : IEqualityComparer<Order>
    {
        // Orders are equal if their names and order numbers are equal.
        public bool Equals(Order x, Order y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the order's properties are equal.
            return x.sWorkOrderNumber == y.sWorkOrderNumber && x.sCustomerOrderName == y.sCustomerOrderName;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(Order order)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(order, null)) return 0;

            //Get hash code for the sCustomerOrderName field if it is not null.
            int hashOrderName = order.sCustomerOrderName == null ? 0 : order.sCustomerOrderName.GetHashCode();

            //Get hash code for the sWorkOrderNumber field.
            int hashOrderCode = order.sWorkOrderNumber.GetHashCode();

            //Calculate the hash code for the order.
            return hashOrderName ^ hashOrderCode;
        }
    }