0

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.

4

4 回答 4

8

Except方法完全按照您的描述进行,因此只需调用ToList以在 a 中获取结果List<T>

return list2.Except(list1).ToList();

您的代码抛出NullReffereceException,因为differ变量未初始化:

    List<string> differ = null;

将其更改为:

    List<string> differ = new List<string>();

但是,我建议使用ToList(). 您Comparator的方法不需要:

        List<string> difference = list2.Except(list1).ToList()
        foreach (var value in difference)
        {
            Console.WriteLine(value);
        }

更新- 整个代码,按预期工作

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();
}

结果

Parks and Recreation
Scandal
于 2013-04-18T13:57:31.673 回答
5

问题在于这一行:

List<string> differ = null;

之后,您尝试将某些内容添加到列表中,但列表是null. 你的例外。

您可以按如下方式更改它:

List<string> differ = new List<string>();

但更简单的是替换这一行:

List<string> difference = Comparator(list1, list2);

经过:

List<string> difference = list1.Except(list2).ToList();
于 2013-04-18T13:59:45.317 回答
1

问题就在这里。

List<string> differ = null;

需要是

List<string> differ = new List<string>();
于 2013-04-18T13:59:10.850 回答
1

利用Except

var difference = list1.Except(list2).ToList();
于 2013-04-18T13:58:42.347 回答