【发布时间】:2020-08-25 14:05:35
【问题描述】:
所以我试图找出 2 个 Person 类型列表之间的区别。这是 person 类:
class Person
{
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(int age, string firstName, string lastName)
{
this.Age = age;
this.FirstName = firstName;
this.LastName = lastName;
}
}
在我的代码中,我创建了 2 个变量,List<Person> list1 和 List<Person> list2。
我用以下变量填充 list1:
list1.Add(new Person(20, "first1", "last1"));
list1.Add(new Person(30, "first2", "last2"));
list1.Add(new Person(40, "first3", "last3"));
list1.Add(new Person(50, "first4", "last4"));
并在 list2 中填写以下内容:
list2.Add(new Person(30, "first2", "last2"));
list2.Add(new Person(50, "first4", "last4"));
我的目标是创建另一个列表 (List<Person> list3),其中包含 list1[0] 和 list[2],因为这不是 list2 中包含的内容。我尝试使用list3 = list1.Except(list2).ToList();,但这只会返回list1 的副本,如果我使用list3 = list2.Except(list1).ToList();,它会返回list2 的副本。我该如何解决这个问题?我使用Except() 对吗?
【问题讨论】:
-
这能回答你的问题吗? Difference between two lists 完全相同的副本,以及 Get the differences between 2 lists 在谷歌搜索 1-2 分钟后可以找到它们