gpt4 book ai didi

c# - 比较两个 List 并仅保留唯一值的简单方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:07 24 4
gpt4 key购买 nike

我正在尝试比较两个列表,并在每个列表中只保留每个列表唯一的项目。你能帮忙吗?

我知道如何做交叉,但我实际上需要做相反的事情

        //Example
List<String> baseListCopy = new List<String>();
baseListCopy.Add("Test1");
baseListCopy.Add("Test2");
baseListCopy.Add("Test3");
baseListCopy.Add("Test4");
baseListCopy.Add("Test5");
baseListCopy.Add("Test6");
baseListCopy.Add("Test7");
baseListCopy.Add("Test8");
baseListCopy.Add("Test9");

List<String> resultListCopy = new List<String>();
resultListCopy.Add("Test1");
resultListCopy.Add("Test2");
resultListCopy.Add("Test3");
resultListCopy.Add("Test40");
resultListCopy.Add("Test90");

//returns only items that are present on both lists
resultListCopy = baseListCopy.Intersect(resultListCopy, StringComparer.InvariantCultureIgnoreCase).ToList();

//How do I return only items that are unique to that list?

最佳答案

你的问题不清楚。

听起来您希望所有项目都恰好出现在一个列表中(异或):

a.Union(b).Except(a.Intersect(b))

关于c# - 比较两个 List<String> 并仅保留唯一值的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9710374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com