我有一个list<message>
包含类型Guid
和DateTime
(以及其他属性)的属性。我想摆脱该列表中Guid
和DateTime
相同(除一个之外)的所有项目。会有时候,这两个属性是相同的列表中的其他项目,但其他性能会有所不同,所以我不能只用.Distinct()
选择不同的列表中的两个属性
List<Message> messages = GetList();
//The list now contains many objects, it is ordered by the DateTime property
messages = from p in messages.Distinct( what goes here?);
这就是我现在所拥有的,但是看起来,就必须有一个更好的办法
List<Message> messages = GetList();
for(int i = 0; i < messages.Count() - 1) //use Messages.Count() -1 because the last one has nothing after it to compare to
{
if(messages[i].id == messages[i+1}.id && messages[i].date == message[i+1].date)
{
messages.RemoveAt(i+1);
{
else
{
i++
}
}
http://stackoverflow.com/questions/489258/linq-distinct-on-a-particular-property – Shyju 2012-08-04 18:39:38
感谢。我不知道为什么当我搜索时我找不到。 – user1304444 2012-08-04 19:34:29
我很高兴乔恩的答案适合你。请注意:您的“当前使用的方法”不会编译,并且(修复编译错误之后)它不能在所有情况下工作 - 取决于元素的顺序,您会得到不同的**(错误)**结果(毕竟,你只是将相邻元素相互比较)。 – Adam 2012-08-04 19:59:39