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

编程入门 行业动态 更新时间:2024-10-23 23:34:40
本文介绍了查找字符串两个列表之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

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

本文发布于:2023-11-02 17:42:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   区别   两个   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!