比较两个列表的值并获得差异

时间:2017-11-15 19:50:12

标签: c#

我创建了一个方法来比较两个列表'价值并输出差异:

public static List<OT_Contact> Comparer(
    List<OT_Contact> security_owner,
    List<OT_Contact> borrower)
{
   List<OT_Contact> nonborrowertrustor = security_owner.Except(borrower).ToList();

   foreach (var nbt_list in nonborrowertrustor)
   {
      var list = nbt_list;
   }

   return nonborrowertrustor;
}

任何人都可以告诉我我的代码是否有问题以及如何将其分配给变量,例如:

var result = Comparer(list1, list2);

1 个答案:

答案 0 :(得分:0)

你为什么不使用Linq?

var trustys = security_owner
    .Where(o => !borrower.Any(b=> b.Name == o.Name /* && compare more attributes from o and b */))
    .ToList();

return trustys;

这将使用

过滤非借款人的安全所有者的所有项目
(b=> b.Name == o.Name && .....)

作为“平等措施”。

示例:

using System.Collections.Generic;
using System.Linq;

internal class Program
{
    public class OT_Contact
    {
        public int Age;
        public string Email;
        public string Name;

        public OT_Contact(string name, string email, int age = int.MinValue)
        {
            Name = name;
            Email = email;
            Age = age;
        }

        public override string ToString()
        {
            return ($"{Name} {Email} " + (Age != int.MinValue ? Age.ToString() : "")).Trim();
        }
    }

    static void Main(string[] args)
    {
        var owner = new List<OT_Contact>
            {
                new OT_Contact("paul","p@o", 5),
                new OT_Contact("paul","pppp@o"),
                new OT_Contact("mani","kkk"),
                new OT_Contact("olaf", "olaf", 22)
            };

        var borr = new List<OT_Contact>
            {
                new OT_Contact("paul","popel@o", 5),
                new OT_Contact("paul","pppp@o"),
                new OT_Contact("mani","kkk",99),
                new OT_Contact("olaf", "", 22)
            };

        var trust = owner.Where(o => !borr.Any(b => b.Name == o.Name && b.Email == o.Email)).ToList();

        System.Console.WriteLine("Owner:\n    " + string.Join("\n    ", owner));
        System.Console.WriteLine("\nBorrower:\n    " + string.Join("\n    ", borr));
        System.Console.WriteLine("\nTrustys:\n    " + string.Join("\n    ", trust));

        System.Console.ReadLine();
    }
}

输出(年龄对于平等并不重要,因此不在Any - 条款内):

Owner:
    paul p@o 5
    paul pppp@o
    mani kkk
    olaf olaf 22

Borrower:
    paul popel@o 5
    paul pppp@o
    mani kkk 99
    olaf  22

Trustys:
    paul p@o 5
    olaf olaf 22

此示例模拟问题中使用的IEnumerable.Except(..)。它会找到owner功能列表中找不到的任何borr。 euqality-condition只需考虑NameEmail。如果它找到borrNameEmail相同的borr,则不会将其输出到结果列表中。

只有owner列表中的任何borr功能都不会影响到结果 - 获取owner以外的任何var onlyBorr = borr.Where(b => !owner.Any(o => o.Name == b.Name && o.Email == b.Email)).ToList(); 功能只需交换列表即可完成:

public class OT_ContactEqualityComparer : IEqualityComparer<OT_Contact>
{
    public bool Equals(OT_Contact x, OT_Contact y)
    {
        if (x == null || y == null)
            return false;

        return (x.Name == y.Name && x.Email == y.Email); 
    }

    public int GetHashCode(OT_Contact c)
        => (c.Name ?? "").GetHashCode() ^ (c.Email ?? "").GetHashCode(); 
}

这个受控示例的IEqualityComparer的可行实现可能是

System.Console.WriteLine("\nIEquality:\n    " + string.Join("\n    ", 

    owner.Except(borr, new OT_ContactEqualityComparer())));

允许使用IEnumerable.Except,如下所示:

while