【发布时间】:2023-04-03 13:00:02
【问题描述】:
我有 LIST1 和 LIST2 并且喜欢比较这两个列表。以下是我的条件..
1-If LIST1 and LIST2 have the same items than add same items to LIST3
2-If LIST1 doesnt contain LIST2 items than add different items to LIST4
3-if LIST2 doesnt contain LIST1 items than add different items to LIST5
可以说我的结果如下所示取决于条件;
LIST1<string> = A,B,C,D
LIST2<string> = A,K,F,C
LIST3<string> = A,C
LIST4<string> = B,D
LIST5<string> = K,F
这是我的代码;
foreach (string src in LIST1)
{
foreach (string trg in LIST2)
{
if (LIST1.ToString() == LIST2.ToString())
{
LIST3.Add(LIST1.ToString());
}
else
{
LIST4.Clear();
foreach (string l3 in LIST1)
{
if (!LIST2.Contains(l3))
LIST4.Add(l3);
}
LIST5.Clear();
foreach (string l4 in LIST2)
{
if (!LIST1.Contains(l4))
{
LIST5.Add(l4);
}
}
}
}
}
【问题讨论】:
-
如果List1 = 1, 2, 2, 2, 3, 3 and List2 = 1, 1, 1, 2, 2, 4,程序需要做什么?您是否执行此操作:List3 =1 2 2 / List4 = 2 3 3 / List5 = 1 1 4。请参阅this image 以了解我的意思。在这种情况下 except() 将不起作用。