2012-05-30 57 views
3

我有兩個列表(ListAListB),這些列表的類型是相同的PersonInfoLogin字段是唯一鍵。獲取2個列表之間的差異

public class PersonInfo 
{ 
    public string Login { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int Age { get; set; } 
    public bool Active { get; set; } 
} 

我想比較這兩個名單:

  1. 我想要得到的項目列表中ListA不提供ListB

  2. 對於兩個列表中的可用項目,我想從ListALogin字段是一個唯一鍵)列出兩個列表之間存在差異的項目。

例如:如果在所述ListALogin「MyLogin」的FirstName的值不匹配與ListB值。具有「我的登錄名」爲Login的項目必須是結果列表的一部分。

示例:如果ListAListB之間的Age爲特定的登錄是不同的,該項目必須是結果

感謝的一部分。

回答

4

比較自定義數據類型lis的對象TS,你將需要實現IEquatable在你的類並覆蓋GetHashCode()

入住這MSDN Link

你的類

public class PersonInfo : IEquatable<PersonInfo> 
    { 
     public string Login { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public int Age { get; set; } 
     public bool Active { get; set; } 

     public bool Equals(PersonInfo other) 
     { 
      //Check whether the compared object is null. 
      if (Object.ReferenceEquals(other, null)) return false; 

      //Check whether the compared object references the same data. 
      if (Object.ReferenceEquals(this, other)) return true; 

      //Check whether the properties are equal. 
      return Login.Equals(other.Login) && FirstName.Equals(other.FirstName) && LastName.Equals(other.LastName) && Age.Equals(other.Age) && Active.Equals(other.Active); 
     } 

     public override int GetHashCode() 
     { 

      int hashLogin = Login == null ? 0 : Login.GetHashCode(); 

      int hashFirstName = FirstName == null ? 0 : FirstName.GetHashCode(); 

      int hashLastName = LastName == null ? 0 : LastName.GetHashCode(); 

      int hashAge = Age.GetHashCode(); 

      int hashActive = Active.GetHashCode(); 

      //Calculate the hash code. 
      return (hashLogin + hashFirstName + hashLastName)^(hashAge + hashActive); 
     } 
    } 

那麼這裏就是你如何使用它(在Pranay的響應中列出)

  List<PersonInfo> ListA = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "2", FirstName = "Jane", LastName = "Morrison", Active = true, Age = 25 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } }; 
      List<PersonInfo> ListB = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James2222", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } }; 

      //Get Items in ListA that are not in ListB 
      List<PersonInfo> FilteredListA = ListA.Except(ListB).ToList(); 

      //To get the difference between ListA and FilteredListA (items from FilteredListA will be removed from ListA) 
      ListA.RemoveAll(a => FilteredListA.Contains(a)); 
4

EDIT

嘗試此soltuion詳細差: Compare two objects and find the differences


How to: Find the Set Difference Between Two Lists (LINQ)

Enumerable.Except Method (IEnumerable, IEnumerable) -Produces通過使用默認的相等比較器對值進行比較兩個序列的差集。

 double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 }; 
     double[] numbers2 = { 2.2 }; 

     IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2); 

//newList will include all the common data between the 2 lists 
List<T> newList = list1.Intersect(list2).ToList<T>(); 


//differences will be the data not found 
List<T> differences = list1.RemoveAll(a => newList.Contains(a)); 

外連接來獲得差異

var compare1to2 = from a in 
      from b in driveList2.Where(b => b.property == a.property).DefaultIfEmpty() 
           select a; 
+0

在任何情況下,我得到我想要的結果。 1.如果ListA有3個項目和ListB 2項目,結果我看不到那個。 2.如果兩個列表中有3個項目(雙方的登錄匹配項)如何比較每個屬性? (RemoveAll不會出現在intellisense中) –

+0

@ Kris-I - 嘗試外部連接由我更新.... –

+0

對不起,但我仍然有3項中的結果,而不是差異 –

-3

你可以使用ZIP()和Intersect()從LINQ

+4

添加一個例子,我會帶走downvote :) – leppie

2
var list3 = list1.Except(list2).ToList(); //List3 contains what in list1 but not _list2. 
0

試試這個吧對象比較和循環周圍List<T>

public static void GetPropertyChanges<T>(this T oldObj, T newObj) 
{ 
    Type type = typeof(T); 
    foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) 
    { 
     object selfValue = type.GetProperty(pi.Name).GetValue(oldObj, null); 
     object toValue = type.GetProperty(pi.Name).GetValue(newObj, null); 
     if (selfValue != null && toValue != null) 
     { 
      if (selfValue.ToString() != toValue.ToString()) 
      { 
       //do your code 
      } 
     } 
    } 
}