找到两个字符串列表之间的区别

Pra*_*bhu 3 .net c# linq string lambda

我很确定这是重复的,但我已经尝试了一切,但我似乎仍然无法得到差异.我有两个字符串列表:listA和listB.我正在尝试查找listA中不在B中的项目.

示例:listA:"1","2","4","7"listB:"2","4"我想要的输出是:"1","7"

这是我尝试的for循环和lambda表达式,但这些需要很长时间:

//these two approaches take too long for huge lists

    foreach (var item in listA)
            {
                if (!listB.Contains(item))
                    diff.Add(id);
            }

    diff = listA.Where(id => !listB.Contains(id)).ToList();

//these don't give me the right differences

    listA.Except(listB).ToList();

    var set = new HashSet<string>(listA);
    set.SymmetricExceptWith(listB);
Run Code Online (Sandbox Code Playgroud)

Yai*_*vet 6

使用LINQ的Except方法:

listA.Except(listB).ToList();
Run Code Online (Sandbox Code Playgroud)