C#比较两个列表,返回列表中的新项目

时间:2019-11-30 10:48:06

标签: c# list

我有2个带有对象的列表。我想比较它们并返回新列表中的所有新对象

我尝试下面的代码,但没有得到答案

 var inInsyt = (from prd in db.COM_CUSTOMER_PRODUCT
                 join inv in db.INS_INVENTORY on prd.COM_CUSTOMER_PRODUCT_ID 
                 equals inv.COM_PRODUCT_ID
                 where prd.COM_CUSTOMER_ID == 5252
                 select new ProductInventoryInfo
                 {
                     sku = prd.PRODUCT_CODE,
                     quantity = inv.INV_AVAILABLE
                 }).ToList();


            var inEComSite = (from qlInv in db.INS_OPENCART_QOOLMART_INVENTORY
                              where qlInv.ID>0
                              select new ProductInventoryInfo
                              {
                                  sku = qlInv.SKU,
                                  quantity = qlInv.QUANTITY
                              }).ToList();

--------- 1st方法------------------------------------ ----------------------------------------

            var firstNotSecond = inInsyt.Except(inEComSite).ToList();
            var secondNotFirst = inEComSite.Except(inInsyt).ToList();

--------------------第二种方法------------------------- ----------------------------------

 List<ProductInventoryInfo> objectList3 = inEComSite.Where(o => inInsyt.Contains(o)).ToList();

 List<ProductInventoryInfo> objectList4 = inInsyt.Where(o => !inEComSite.Contains(o)).ToList();

4 个答案:

答案 0 :(得分:3)

您应该为IEqualityComparer类实现ProductInventoryInfo

看看here

答案 1 :(得分:1)

如果您有两个字符串或数字列表,则可以使用以下方法进行比较

主要方法

0

如果两个列表是 objecs ,则可以使用以下方法进行比较

ProductInventoryInfo

Theta(n 2**n)

扩展方法以比较对象

var list1 = new List<string>
        {
            "Product 1",
            "Product 2",
            "Product 3",
            "Product 4"
        };

var list2 = new List<string>
        {
            "Product 2",
        };

var list3 = list1.Where(i => list2.All(x => x != i)).ToList();

var list4 = list1.Except(list2).ToList();

主要方法

public class ProductInventoryInfo
{
    public string ProductName { get; set; }

    public override bool Equals(object obj)
    {
        if (!(obj is ProductInventoryInfo))
        {
            return false;
        }

        var other = (ProductInventoryInfo)obj;
        return this.ProductName == other.ProductName;
    }

    protected bool Equals(ProductInventoryInfo other)
    {
        return ProductName == other.ProductName;
    }

    public override int GetHashCode()
    {
        return (ProductName != null ? ProductName.GetHashCode() : 0);
    }
}

答案 2 :(得分:0)

请参见以下代码段:

        List<int> firstList = new List<int>() { 1,2,2,3,3 };
        List<int> secondList = new List<int>() { 1 };
        List<int> newList = new List<int>();

        var firstNotSecond = firstList.Except(secondList).ToList();
        var secondNotFirst = secondList.Except(firstList).ToList();

        newList.AddRange(firstNotSecond);
        newList.AddRange(secondNotFirst);

输出newList:{2,3}

答案 3 :(得分:0)

如果您将对象添加到相同类型的列表中,这将非常有效

var differences = list2.Where(l2 => 
    !list1.Any(l1 => l1.sku == l2.sku && l1.quantity == l2.quantity ));

或者(如果您愿意)

 var differences = list2.Where(l2 => 
        !list1.Any(l1 => l1.sku == l2.sku || l1.quantity == l2.quantity ));