比较两个List <String>的简单方法,只保留唯一值

And*_*rew 1 c# list data-structures

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

我知道怎么做交集,但实际上我需要做相反的事情

        //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?
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 7

你的问题不清楚.

听起来你想要所有出现在一个列表中的项目(XOR):

a.Union(b).Except(a.Intersect(b))
Run Code Online (Sandbox Code Playgroud)