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

由网友(两不疑.)分享简介:我是pretty的肯定,这是一个重复的,但我已经尝试了一切,而我似乎仍不能得到的差异。我有一个字符串两个列表:为listA和数组listB。我试图找到为listA不在B. 物品例:listA的:1,2,4,7数组listB:2,4我想输出是:1,7下面是一个for循环和lambda EX pression,我试过...

我是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);
 

解决方案 分别将字符串a和字符串b中的字符倒置.....问题,急求大神帮忙

使用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();

阅读全文

相关推荐

最新文章