I have two List. Which content simple string values. As example:
IList1 content:
- The Avengers
- Shutter Island
- Inception
- The Dark Knight Rises
List2 content:
- 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.
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.