2011-11-13 99 views
2
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" } 
    }; 

作爲輸出我想有列表1 +列表2 =加入兩個列表比較它們的元素的屬性

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進行比較,並使用日期更長的ond。如果ChangeDate是eqal,則採取其中任何一種,但不能同時採用兩種。 也許它更容易連接兩個列表,而不是用lambda過濾它們。我試過了,但總是出來一些難看的代碼:/

任何人有什麼想法?

+0

如果可以的話,我會用'DateTime'而不是字符串值。 –

回答

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

+1,比我建議的方式簡單得多! –

+0

就是這樣。非常感謝。我已經應用瞭解決方案,但忘記標記anwser。對不起,再次感謝! – 100r

0

合併這兩個列表按降序排序。現在您需要在排序列表中首次出現每個ID。

1

這樣的事情呢?

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); 
     } 
    } 
} 
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而遍歷集合,這對我來說似乎是合理的。

它也沒有錯誤檢查。如果任何值可能爲空,或者ChangeDate可能無效,則此代碼將拋出異常。如果這可能是一個問題,我建議你在傳入數據之前檢查你的數據,並且你在PersonIdEqualityComparer類中做錯誤檢查。