[英]C# Compare two lists, return the new items in 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();
--------第一種方法------------------------------------ ------------------------------------
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();
您應該為ProductInventoryInfo
類實現一個IEqualityComparer
。
看看這里
如果你有兩個字符串或數字列表,你可以使用下面的方法來比較
主要方法
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();
如果兩個列表是對象,您可以使用以下方法進行比較
類ProductInventoryInfo
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);
}
}
比較對象的擴展方法
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
}
}
主要方法
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);
}
});
});
請參閱以下代碼段:
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);
輸出新列表:{2,3}
如果您將對象添加到相同類型的列表中,這將非常有效
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 ));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.