C# 如何比较两个列表内容数据并返回不匹配值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16084959/
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
How to compare two List content data and return mismatch value?
提问by Anudeep
I have two List. Which content simple string values. As example:
我有两个列表。其中内容简单的字符串值。例如:
IList1 content:
IList1 内容:
- The Avengers
- Shutter Island
- Inception
- The Dark Knight Rises
- 复仇者
- 禁闭岛
- 成立
- 黑暗骑士崛起
List2 content:
清单2内容:
- The Avengers
- Shutter Island
- Inception
- The Dark Knight Rises
- Parks and Recreation
- Scandal
- 复仇者
- 禁闭岛
- 成立
- 黑暗骑士崛起
- 公园和休闲
- 丑闻
I want to compare two list and it will return mismatch value. Like in this case it will return "Parks and Recreation" and "Scandal" as they are not matching with the values of List1.
我想比较两个列表,它将返回不匹配值。就像在这种情况下一样,它将返回“公园和娱乐”和“丑闻”,因为它们与 List1 的值不匹配。
I tried it. But it throws exception "Object reference not set to an instance of an object".
我尝试过这个。但它抛出异常“对象引用未设置为对象的实例”。
static void Main(string[] args)
{
List<string> list1 = new List<string>();
list1.Add("The Avengers");
list1.Add("Shutter Island");
list1.Add("Inception");
list1.Add("The Dark Knight Rises");
List<string> list2 = new List<string>();
list2.Add("The Avengers");
list2.Add("Shutter Island");
list2.Add("Inception");
list2.Add("The Dark Knight Rises");
list2.Add("Parks and Recreation");
list2.Add("Scandal");
try
{
List<string> difference = Comparator(list1, list2);
foreach (var value in difference)
{
Console.WriteLine(value);
}
}
catch (System.NullReferenceException e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
public static List<string> Comparator(List<string> list1, List<string> list2)
{
IEnumerable<string> differenceQuery = list1.Except(list2);
List<string> differ = null;
foreach (string s in differenceQuery)
differ.Add(s);
return differ;
}
Can anyone help me out? Thanks in advance.
谁能帮我吗?提前致谢。
采纳答案by Daniel A.A. Pelsmaeker
The problem is with this line:
问题出在这一行:
List<string> differ = null;
After that you try to add something to the list, but the list is null. There's your exception.
之后,您尝试向列表中添加一些内容,但列表是null. 有你的例外。
You could change it as follows:
您可以按如下方式更改它:
List<string> differ = new List<string>();
But easier is to replace this line:
但更简单的是替换这一行:
List<string> difference = Comparator(list1, list2);
By:
经过:
List<string> difference = list1.Except(list2).ToList();
回答by MarcinJuraszek
Exceptmethod does exactly what you described, so just call ToListto get results in a List<T>:
Except方法完全符合您的描述,因此只需调用ToList以获取结果List<T>:
return list2.Except(list1).ToList();
Your code throws NullReffereceExceptionbecausediffervariable is not initialized:
您的代码抛出,NullReffereceException因为differ变量未初始化:
List<string> differ = null;
change it to:
将其更改为:
List<string> differ = new List<string>();
However, I would suggest using ToList(). YouComparatormethod won't be necessary than:
但是,我建议使用ToList(). 你的Comparator方法不需要比:
List<string> difference = list2.Except(list1).ToList()
foreach (var value in difference)
{
Console.WriteLine(value);
}
Update- whole code, working as expected
更新- 整个代码,按预期工作
static void Main(string[] args)
{
List<string> list1 = new List<string>();
list1.Add("The Avengers");
list1.Add("Shutter Island");
list1.Add("Inception");
list1.Add("The Dark Knight Rises");
List<string> list2 = new List<string>();
list2.Add("The Avengers");
list2.Add("Shutter Island");
list2.Add("Inception");
list2.Add("The Dark Knight Rises");
list2.Add("Parks and Recreation");
list2.Add("Scandal");
List<string> difference = list2.Except(list1).ToList();
foreach (var value in difference)
{
Console.WriteLine(value);
}
Console.ReadLine();
}
Result
结果
Parks and Recreation
Scandal
回答by Praveen Nambiar
Use Except
用 Except
var difference = list1.Except(list2).ToList();
回答by cgotberg
The problem is right here.
问题就在这里。
List<string> differ = null;
Needs to be
需要是
List<string> differ = new List<string>();

