C# 比较两个 list<string> 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15612162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare the difference between two list<string>
提问by Max
I'am trying to check the difference between two List<string>
in c#
.
我'尝试检查两者之间的差别List<string>
在c#
。
Example:
例子:
List<string> FirstList = new List<string>();
List<string> SecondList = new List<string>();
The FirstList
is filled with the following values:
在FirstList
充满了以下值:
FirstList.Add("COM1");
FirstList.Add("COM2");
The SecondList
is filled with the following values:
在SecondList
充满了以下值:
SecondList.Add("COM1");
SecondList.Add("COM2");
SecondList.Add("COM3");
Now I want to check if some values in the SecondList
are equal to values in the FirstList
.
现在我想检查 中的某些值SecondList
是否等于FirstList
.
If there are equal values like: COM1 and COM2, that are in both lists, then filter them from the list, and add the remaining values to another list.
如果两个列表中存在相同的值,例如:COM1 和 COM2,则从列表中过滤它们,并将剩余的值添加到另一个列表中。
So if I would create a new ThirdList
, it will be filled with "COM3" only, because the other values are duplicates.
因此,如果我要创建一个 new ThirdList
,它将仅填充“COM3”,因为其他值是重复的。
How can I create such a check?
我怎样才能创建这样的支票?
采纳答案by Ilya Ivanov
Try to use ExceptLINQ extension method, which takes items only from the first list, that are not present in the second. Example is given below:
尝试使用ExceptLINQ 扩展方法,该方法仅从第一个列表中获取项目,而第二个列表中不存在这些项目。示例如下:
List<string> ThirdList = SecondList.Except(FirstList).ToList();
You can print the result using the following code:
您可以使用以下代码打印结果:
Console.WriteLine(string.Join(Environment.NewLine, ThirdList));
Or
或者
Debug.WriteLine(string.Join(Environment.NewLine, ThirdList));
Note: Don't forget to include: using System.Diagnostics;
注意:不要忘记包括: using System.Diagnostics;
prints:
印刷:
COM3
回答by Tim Schmelter
You can use Enumerable.Intersect
:
您可以使用Enumerable.Intersect
:
var inBoth = FirstList.Intersect(SecondList);
or to detect strings which are only in one of both lists, Enumerable.Except
:
或检测仅在两个列表之一中的字符串,Enumerable.Except
:
var inFirstOnly = FirstList.Except(SecondList);
var inSecondOnly = SecondList.Except(FirstList);
To get your ThirdList
:
要获得您的ThirdList
:
List<string> ThirdList = inSecondOnly.ToList();
回答by Pranay Rana
Than for this king of reuqirement you can can make use of Except
function.
比起这个需求之王你可以利用Except
功能。
List<string> newlist = List1.Except(List2).ToList();
or you can do this , so the below one create new list three which contains items that are not common in list1 and list2
或者你可以这样做,所以下面的一个创建新的列表三,其中包含列表 1 和列表 2 中不常见的项目
var common = List1.Intersect(List2);
var list3 = List1.Except(common ).ToList();
list3.AddRange(List2.Except(common ).ToList());
the above one is help full when list1 and list2 has differenct item like
当 list1 和 list2 有不同的项目时,上面的一个是有帮助的
List<string> list1= new List<string>();
List<string> list2 = new List<string>();
The FirstList is filled with the following values:
FirstList 填充有以下值:
list1.Add("COM1");
list1.Add("COM2");
list1.Add("COM4");
The SecondList is filled with the following values:
SecondList 填充了以下值:
list2 .Add("COM1");
list2 .Add("COM2");
list2 .Add("COM3");
by using above code list3 contains COM4 and COM3.
通过使用上面的代码 list3 contains COM4 and COM3.