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" }
};
作为输出,我想有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" }
算法是这样的。 加入两个清单。如果列表的元素具有相同的ID,请通过ChangeDate进行比较,并使用更大的日期。如果ChangeDate是equeal采取其中任何一个,但不是两个。 也许它更容易连接两个列表,而不是用lambda过滤它们。我试过了,但总是拿出一些丑陋的代码:/
有人有任何想法吗?
答案 0 :(得分:4)
LINQ
var q = from person in list1.Concat(list2)
group person by person.ID into g
select g.OrderByDescending(p => p.ChangeDate).First();
答案 1 :(得分:0)
合并两个列表在降序日期排序。现在您需要在排序列表中首先出现每个id。
答案 2 :(得分:0)
这样的事情怎么样?
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public int ID;
public string Name;
public DateTime ChangeDate;
}
public class PersonComparer : IEqualityComparer<Person>
{
public bool Equals(Person p1, Person p2)
{
return p1.ID == p2.ID;
}
public int GetHashCode(Person p)
{
return p.ID.GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
var list1 = new List<Person>
{
new Person { ID = 1, Name = "Peter", ChangeDate = DateTime.Parse("2011-10-21") },
new Person { ID = 2, Name = "John", ChangeDate = DateTime.Parse("2011-10-22") },
new Person { ID = 3, Name = "Mike", ChangeDate = DateTime.Parse("2011-10-23") },
new Person { ID = 4, Name = "Dave", ChangeDate = DateTime.Parse("2011-10-24") }
};
var list2 = new List<Person>
{
new Person { ID = 1, Name = "Pete", ChangeDate = DateTime.Parse("2011-10-21") },
new Person { ID = 2, Name = "Johny", ChangeDate = DateTime.Parse("2011-10-20") },
new Person { ID = 3, Name = "Mikey", ChangeDate = DateTime.Parse("2011-10-24") },
new Person { ID = 5, Name = "Larry", ChangeDate = DateTime.Parse("2011-10-27") }
};
var pc = new PersonComparer();
var combined = list1.Join(list2, p => p.ID, p => p.ID, (p1,p2) => p2.ChangeDate > p1.ChangeDate ? p2 : p1)
.Union(list1.Except(list2, pc))
.Union(list2.Except(list1, pc));
foreach(var p in combined)
{
Console.WriteLine(p.ID + " " + p.Name + " " + p.ChangeDate);
}
}
}
答案 3 :(得分:0)
您可以连接并对它们进行排序,然后获得不同的值:
class PersonIdEqualityComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
return x.ID == y.ID;
}
public int GetHashCode(Person person)
{
return person.ID;
}
}
var result = list1.Concat(list2)
.OrderByDescending(i => DateTime.Parse(i.ChangeDate)) // Most recent first
.Distinct(new PersonIdEqualityComparer())
;
这假设Distinct
将获取每个集合中遇到的第一个项目而不是任意项目。考虑到它可能只是在遍历集合时将它们插入HashSet
,这对我来说似乎是合理的。
它也没有错误检查。如果任何值可能为null,或者ChangeDate
可能无效,则此代码将抛出异常。如果这可能是个问题,我建议您在传入之前检查数据,并在PersonIdEqualityComparer
类中进行错误检查。