查找字符串两个列表之间的区别 [英] Finding the difference between two lists of strings

查看:191
本文介绍了查找字符串两个列表之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty的肯定,这是一个重复的,但我已经尝试了一切,而我似乎仍不能得到的差异。我有一个字符串两个列表:为listA和数组listB。我试图找到为listA不在B.

物品

例: listA的:1,2,4,7 数组listB:2,4 我想输出是:1,7

下面是一个for循环和lambda EX pression,我试过了,但这些都需要很长一段时间:

  //这两种方法需要很长时间的庞大名单

    的foreach(在VAR listA的项目)
            {
                如果(!listB.Contains(项目))
                    diff.Add(ID);
            }

    的diff = listA.Where(ID =>!listB.Contains(ID))了ToList()。

//这些不给我正确的差别

    listA.Except(数组listB).ToList();

    VAR组=新的HashSet<字符串>(listA的);
    set.SymmetricExceptWith(数组listB);
 

解决方案

使用LINQ的除了方法:

  listA.Except(数组listB).ToList();
 

I'm pretty sure this is a duplicate, but I have tried everything, and I still cannot seem to get the differences. I have two lists of strings: listA and listB. I'm trying to find items in listA that are not in B.

Example: listA: "1", "2", "4", "7" listB: "2", "4" The output I want is: "1", "7"

Here is a for loop and lambda expression that I tried, but these take a really long time:

//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);

解决方案

Use LINQ's Except method:

listA.Except(listB).ToList();

这篇关于查找字符串两个列表之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆