Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较两个列表,返回列表中的新项目_C#_List - Fatal编程技术网

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

C# 比较两个列表,返回列表中的新项目,c#,list,C#,List,我有两个对象列表。我想比较它们并返回新列表中的所有新对象 我尝试下面的代码,但没有得到回答 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_

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

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

 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();
---------第一种方法----------------------------------------------------------------------------

            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();
List objectList3=inEComSite.Where(o=>inInsyt.Contains(o)).ToList();
List objectList4=inInsyt.Where(o=>!inEComSite.Contains(o)).ToList();


您应该为
ProductInventoryInfo
类实现一个
IEqualityComparer

请看下面的片段:

        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);
List firstList=newlist(){1,2,2,3,3};
List secondList=新列表(){1};
List newList=新列表();
var firstNotSecond=firstList.Except(secondList.ToList();
var secondNotFirst=secondList.Except(firstList.ToList();
newList.AddRange(firstNotSecond);
newList.AddRange(secondNotFirst);

output newList:{2,3}

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

var differences = list2.Where(l2 => 
    !list1.Any(l1 => l1.sku == l2.sku && l1.quantity == l2.quantity ));
或者(如果您愿意)


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

Main方法

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();
 var list1 = new List<ProductInventoryInfo>
        {
            new ProductInventoryInfo{ ProductName="Product 1"},
            new ProductInventoryInfo{ ProductName="Product 2"},
            new ProductInventoryInfo{ ProductName="Product 3"},
            new ProductInventoryInfo{ ProductName="Product 4"},
        };

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

 var list3 = list1.Where(x => !list2.ObjectsAreEqual(x)).ToList(); //use Extensions Method

 //use override Equals
 var list4 = new List<ProductInventoryInfo>();

 list1.ForEach(x =>
     {
       list2.ForEach(y =>
          { 
            if (!x.Equals(y))
              {
                 list4.Add(x);
              }
          });
      });
比较对象的扩展方法

public static class ExtensionsMethod
{
    public static bool ObjectsAreEqual(this IEnumerable<ProductInventoryInfo> items, ProductInventoryInfo obj2)
    {
        return items.Any(productInventoryInfo => ObjectsAreEqual<ProductInventoryInfo>(productInventoryInfo, obj2));
    }

    private static bool ObjectsAreEqual<T>(T obj1, T obj2)
    {
        return JsonConvert.SerializeObject(obj1) == JsonConvert.SerializeObject(obj2);//convert object to json by Newtonsoft.Json and compare that
    }
}
公共静态类扩展方法
{
公共静态bool ObjectsAreEqual(此IEnumerable items,ProductInventoryInfo obj2)
{
返回项目.Any(productInventoryInfo=>ObjectsAreEqual(productInventoryInfo,obj2));
}
私有静态bool ObjectsAreEqual(T obj1,T obj2)
{
返回JsonConvert.SerializeObject(obj1)=JsonConvert.SerializeObject(obj2);//通过Newtonsoft.json将对象转换为json并进行比较
}
}
Main方法

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();
 var list1 = new List<ProductInventoryInfo>
        {
            new ProductInventoryInfo{ ProductName="Product 1"},
            new ProductInventoryInfo{ ProductName="Product 2"},
            new ProductInventoryInfo{ ProductName="Product 3"},
            new ProductInventoryInfo{ ProductName="Product 4"},
        };

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

 var list3 = list1.Where(x => !list2.ObjectsAreEqual(x)).ToList(); //use Extensions Method

 //use override Equals
 var list4 = new List<ProductInventoryInfo>();

 list1.ForEach(x =>
     {
       list2.ForEach(y =>
          { 
            if (!x.Equals(y))
              {
                 list4.Add(x);
              }
          });
      });
var list1=新列表
{
新产品InventoryInfo{ProductName=“产品1”},
新产品InventoryInfo{ProductName=“产品2”},
新产品InventoryInfo{ProductName=“产品3”},
新产品InventoryInfo{ProductName=“产品4”},
};
var list2=新列表
{
新产品InventoryInfo{ProductName=“产品2”},
};
var list3=list1.Where(x=>!list2.ObjectsAreEqual(x)).ToList()//使用扩展方法
//使用覆盖等于
var list4=新列表();
列表1.ForEach(x=>
{
列表2.ForEach(y=>
{ 
如果(!x等于(y))
{
清单4.添加(x);
}
});
});

如果要比较某些自定义数据类型的对象序列,必须在助手类中实现IEquatable泛型接口。请检查下面我提交了一个答案谢谢您的回复,对不起,我尝试了上面的方法,但是我的答案错了。如果您将对象添加到同一类型的列表中,这将非常有效。((var differences=list2.Where(l2=>!list1.Any(l1=>l1.sku==l2.sku&&l1.quantity==l2.quantity));)