Join two list comparing their elements properties
- by 100r
public class Person()
{
int ID;
string Name;
DateTime ChangeDate
}
var list1 = new List<Person>
{
new Person { ID= 1, Name = "Peter", ChangeDate= "2011-10-21" },
new Person { ID= 2, Name = "John", ChangeDate= "2011-10-22" },
new Person { ID= 3, Name = "Mike", ChangeDate= "2011-10-23" },
new Person { ID= 4, Name = "Dave", ChangeDate= "2011-10-24" }
};
var list2 = new List<Person>
{
new Person { ID= 1, Name = "Pete", ChangeDate= "2011-10-21" },
new Person { ID= 2, Name = "Johny", ChangeDate= "2011-10-20" },
new Person { ID= 3, Name = "Mikey", ChangeDate= "2011-10-24" },
new Person { ID= 5, Name = "Larry", ChangeDate= "2011-10-27" }
};
As output I would like to have list1 + list2 =
Person { ID= 1, Name = "Peter", ChangeDate= "2011-10-21" },
Person { ID= 2, Name = "John", ChangeDate= "2011-10-22" },
Person { ID= 3, Name = "Mikey", ChangeDate= "2011-10-24" },
Person { ID= 4, Name = "Dave", ChangeDate= "2011-10-24" }
Person { ID= 5, Name = "Larry", ChangeDate= "2011-10-27" }
And the Algorithm is like this.
Join two list. If elements of lists have same ID, compare them by ChangeDate and take the ond with bigger date. If ChangeDate are equeal take any of them but not both.
Maybe its easier to concat both lists and than to filter them with lambda. I tried, but always came out with some ugly code :/
Anyone have any idea?