比较两个列表,但不包括c#中的特定列

时间:2019-02-05 08:03:01

标签: c# linq

考虑一个名为Employee的实体,其中包含id,age和name作为属性

我有两个包含员工详细信息的列表

我必须比较ID列除外的两个列表

请帮助您提出建议

3 个答案:

答案 0 :(得分:2)

这将产生两个列表中相同的所有条目,而忽略员工的Id属性:

var employees1 = new List<Employee>
{
    new Employee(1, "Thomas", 12),
    new Employee(2, "Alex", 24),
    new Employee(3, "Tobias", 13),
    new Employee(4, "Joshua", 12),
    new Employee(5, "Thomas", 24)
};

var employees2 = new List<Employee>
{
    new Employee(1, "Thomas", 12),
    new Employee(2, "Yu", 24),
    new Employee(3, "Max", 13),
    new Employee(4, "Joshua", 30),
    new Employee(5, "Maico", 13)
};

var duplicates = employees1.Intersect(employees2, new EmployeeComparer());



class EmployeeComparer : IEqualityComparer<Employee>
{
    public bool Equals(Employee employee1, Employee employee2)
    {
        if (Object.ReferenceEquals(employee1, null) || Object.ReferenceEquals(employee2, null) ||
            Object.ReferenceEquals(employee1, employee2)) return false;

        return employee1.Name == employee2.Name && employee1.Age == employee2.Age;
    }

    public int GetHashCode(Employee employee)
    {
        return 0;
    }
}

class Employee
{
    public Employee(int id, string name, int age)
    {
        Id = id;
        Name = name;
        Age = age;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

答案 1 :(得分:0)

由于该帖子被LINQ标记,因此我在答案中使用了它。

static void Main(string[] args)
{
   var list1 = new List<Person>();
   var list2 = new List<Person>();

   list1.Add(new Person(1, "james", "moon"));
   list1.Add(new Person(1, "bob", "bar"));
   list1.Add(new Person(1, "tim", "lane"));

   list1.Add(new Person(1, "fizz", "sea"));
   list2.Add(new Person(1, "buzz", "space"));
   list2.Add(new Person(1, "james", "moon"));

   var result = findDuplicates(list1, list2);
}


public static List<Person> findDuplicates(List<Person> l1, List<Person> l2)
{
   return l1.Where(p => l2.Any(z => z.FName == p.FName && z.Addre == p.Addre)).ToList();
}

人员班

public class Person
{
  private int id;
  private string fName;
  private string addre;

  public string Addre
  {
     get { return addre; }
     set { addre = value; }
  }

  public string FName
  {
     get { return fName; }
     set { fName = value; }
  }

  public int ID
  {
     get { return id; }
     set { id = value; }
  }

  public Person(int i, string f, string a)
  {
     ID = i;
     FName = f;
     Addre = a;
  }
 }

答案 2 :(得分:0)

假设 Employee 类:

class Employee
{
    public int id { get; set; }
    public int age { get; set; }
    public string name { get; set; }
}

您可以简单地使用相交

        var list1 = new List<Employee> {
   new Employee{ id=2 , age=23, name="Hari"},
   new Employee{ id=3 , age=10, name="Joe"},
   new Employee{ id=4 , age=29, name="Daniel"},
};
        var list2 = new List<Employee> {
   new Employee{ id=1 , age=23, name="Hari"},
   new Employee{ id=5 , age=10, name="Joe"},
   new Employee{ id=6 , age=29, name="Daniel"},
};

var intersect = list1.Select(e => new { e.age, e.name }).Intersect(list2.Select(e => new { e.age, e.name })).ToList();