简体   繁体   English

比较两个列表

[英]Comparing two Lists

Both List A and List 列表A和列表

List A = new List(); 列表A =新列表(); List B = new List(); 清单B =新清单();

For the sake of simplicity, I have mentioned the Products in each List. 为简单起见,我在每个列表中都提到了产品。 Te numbers here are referring to the Product ID. 这里的数字是指产品ID。

  List A contains 1,2,3,4,5

  List B contains 1,2,3,4,5,6,7

I want to know the code in c# asp.net that will compare the two lists, and synchronizing them. 我想知道c#asp.net中的代码,它将比较两个列表并同步它们。 Meaning if List B had some more Products (lets say 19,20) it copy 19,20 to List A. 意思是如果列表B有更多的产品(比方说19,20)它将19,20复制到列表A.

Lets say it was a List, how would I compare the qty attribute. 让我们说它是一个List,我如何比较qty属性。 I have to check if the same product ID is in both lists. 我必须检查两个列表中是否有相同的产品ID。 Then it would check the qty attribute. 然后它会检查qty属性。 It would synch them both. 它会使它们同步。

List<T> result = firstList.Except(secondList).ToList()

而且您可能需要在类上实现EqualityComparer<T>以确保对象的相等性。

不确定这是否是最有效的解决方案(我确信必须有更好的Linq方法)...

c = A.Except(B).Union(B.Except(A)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM