C# 比较两个列表-如果一个列表中的任何对象属性已更改或列表中添加了新对象,请返回该对象

C# 比较两个列表-如果一个列表中的任何对象属性已更改或列表中添加了新对象,请返回该对象,c#,linq,list,compare,C#,Linq,List,Compare,假设我有一门课: public class Product { public string Id { get; set; } public int Quantity { get; set; } } 然后我有两个列表: var oldList = new List<Product>(){ new Product(){ Id = "1", Quantity = 1 } }; var newList =

假设我有一门课:

public class Product 
{
    public string Id { get; set; }
    public int Quantity { get; set; }
}
然后我有两个列表:

var oldList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        }
      };

var newList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 5
        }
      };

如果一个新项目被添加到
新列表中
,那么我想返回该项目(Id为=“2”)的产品对象

您可以尝试以下方法:

var oldList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        }
      };

var newList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        },
        new Product(){
          Id = "2", Quantity = 1
        }
      };
var result = newList.Except(oldList);
public List<MyItems> GetItemsFromANotInThatAreNotInB(List<MyItems> A, List<MyItems> B)
{
    return (from b in B
            where !(from a in A select a.Id).Contains(b.Id)
            select b).ToList();
}
但是您必须首先为
产品
类实现
IEquatable
接口

public class Product : IEquatable<Product> 
{
    public string Id { get; set; }
    public int Quantity { get; set; }

    public bool Equals(Product product)
    {
        if (product == null)
        {
            return false;
        }

        return (Id == product.Id) && (Quantity == product.Quantity);
    }
}
公共类产品:IEquatable
{
公共字符串Id{get;set;}
公共整数数量{get;set;}
公共布尔等于(产品)
{
如果(产品==null)
{
返回false;
}
退货(Id==product.Id)&(数量==product.Quantity);
}
}

您可以尝试以下方法:

var oldList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        }
      };

var newList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        },
        new Product(){
          Id = "2", Quantity = 1
        }
      };
var result = newList.Except(oldList);
public List<MyItems> GetItemsFromANotInThatAreNotInB(List<MyItems> A, List<MyItems> B)
{
    return (from b in B
            where !(from a in A select a.Id).Contains(b.Id)
            select b).ToList();
}
但是您必须首先为
产品
类实现
IEquatable
接口

public class Product : IEquatable<Product> 
{
    public string Id { get; set; }
    public int Quantity { get; set; }

    public bool Equals(Product product)
    {
        if (product == null)
        {
            return false;
        }

        return (Id == product.Id) && (Quantity == product.Quantity);
    }
}
公共类产品:IEquatable
{
公共字符串Id{get;set;}
公共整数数量{get;set;}
公共布尔等于(产品)
{
如果(产品==null)
{
返回false;
}
退货(Id==product.Id)&(数量==product.Quantity);
}
}

首先,您应该实现相等比较器来比较两个产品项是否相等:

class ProductEqualityComparer : IEqualityComparer<Product>
{
    public bool Equals(Product x, Product y)
    {
        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.Id == y.Id && x.Quantity == y.Quantity;
    }

    public int GetHashCode(Product product)
    {
        if (Object.ReferenceEquals(product, null)) return 0;

        return product.Id.GetHashCode() ^ product.Quantity.GetHashCode();
    }
}

首先,您应该实现相等比较器来比较两个产品项是否相等:

class ProductEqualityComparer : IEqualityComparer<Product>
{
    public bool Equals(Product x, Product y)
    {
        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.Id == y.Id && x.Quantity == y.Quantity;
    }

    public int GetHashCode(Product product)
    {
        if (Object.ReferenceEquals(product, null)) return 0;

        return product.Id.GetHashCode() ^ product.Quantity.GetHashCode();
    }
}

一种解决方法,因此您不必使用,只需使用Linq来处理以下对象:

var oldList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        }
      };

var newList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        },
        new Product(){
          Id = "2", Quantity = 1
        }
      };
var result = newList.Except(oldList);
public List<MyItems> GetItemsFromANotInThatAreNotInB(List<MyItems> A, List<MyItems> B)
{
    return (from b in B
            where !(from a in A select a.Id).Contains(b.Id)
            select b).ToList();
}
public List GetItemsFromANotInThatAreNotInB(列表A、列表B)
{
返回(从b中的b返回)
其中!(从a中选择a.Id)。包含(b.Id)
选择b).ToList();
}

一种变通方法,因此您不必使用,只需使用Linq来处理以下对象:

var oldList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        }
      };

var newList = new List<Product>(){
        new Product(){
          Id = "1", Quantity = 1
        },
        new Product(){
          Id = "2", Quantity = 1
        }
      };
var result = newList.Except(oldList);
public List<MyItems> GetItemsFromANotInThatAreNotInB(List<MyItems> A, List<MyItems> B)
{
    return (from b in B
            where !(from a in A select a.Id).Contains(b.Id)
            select b).ToList();
}
public List GetItemsFromANotInThatAreNotInB(列表A、列表B)
{
返回(从b中的b返回)
其中!(从a中选择a.Id)。包含(b.Id)
选择b).ToList();
}

var diffList=oldList.Where(o=>!newList.Any(n=>n.Id==o.Id)).Union(newList.Where(n=>!oldList.Any(o=>n.Id==o.Id))
var diffList=oldList.Where(o=>!newList.Any(n=>n.Id==o.Id)).Union(newList.Where(n=>!oldList.Any(o=>n.Id==o.Id))