2015-10-19 72 views
0

我需要將來自DB的對象的當前列表與新對象列表進行比較。我想比較它們並突出顯示已更改的用戶(在這種情況下,返回TRUE表示它們不同)。如何比較可空對象

由於一些我的對象是空的,這涉及到很多IF NOT的是關於NewObj和CurrentObj側面沒什麼......我一直試圖找到寫下面的,因爲我有一個更有效的方法用它來比較約30個不同類型,即日期,小數,INT,等一個對象..

下面的作品,直到說無論是obj的無等級,因此沒有

建議?

Dim Rank As Boolean = CompareData(NewObj, CurrentObj, "Rank") 
Dim Regiment As Boolean = CompareData(NewObj, CurrentObj, "Rank") 
Dim DateofBirth As Boolean = CompareData(NewObj, CurrentObj, "DoB") 

Private Function CompareData(NewObj As Business.Casualty, CurrentObj As Business.Casualty, FieldToComapre As String) As Boolean 

Select Case FieldToComapre 
     Case "DateOfBirth" 
      Return (Nullable.Equals(NewCasualty.DateOfBirth, CurrentCasualty.DateOfBirth)) 
     Case "Age" 
      Return (Nullable.Equals(NewCasualty.Age, CurrentCasualty.Age))    
     Case "Rank" 
      Return (Nullable.Equals(NewCasualty.Rank.ID, CurrentCasualty.Rank.ID)) 
     Case "Regiment" 
      Return (Nullable.Equals(NewCasualty.Regiment.ID, CurrentCasualty.Regiment.ID))   
     Case Else 
      Return True 
    End Select 

End Function 

回答

0

我真的結束了剛剛與擴展方法..它更簡單和更清潔IMO。

Dim Rank As Boolean = CompareData(NewObj, CurrentObj, "Rank") 
Dim Regiment As Boolean = CompareData(NewObj, CurrentObj, "Regiment") 
Dim Trade As Boolean = CompareData(NewObj, CurrentObj, "Trade") 

Private Function CompareData(NewObj As Business.Casualty, CurrentObj As Business.Casualty, FieldToComapre As String) As Boolean 

Select Case FieldToComapre 
     Case "Trade" 
      Return NewCasualty.Trade.NullableEquals(CurrentCasualty.Trade) 
     Case "Rank" 
      Return NewCasualty.Rank.NullableEquals(CurrentCasualty.Rank) 
     Case "Regiment" 
      Return NewCasualty.Regiment.NullableEquals(CurrentCasualty.Regiment)      
    Case Else 
     Return True 
End Select 

End Function 

public static class NullableCompare 
{ 
    public static bool NullableEquals<T>(this T s1, T s2) 
           where T : class 
    { 
     if (s1 == null) 
     { 
      return s2 == null; 
     } 

     return s1.Equals(s2); 
    } 

} 
public partial class Rank 
{ 
public override bool Equals(object obj) 
    { 
     var p2 = obj as Rank; 

     if (p2 == null) 
     { 
      return false; 
     } 

     if (this.ID != p2.ID) 
     { 
      return false; 
     } 

     return this.ID == p2.ID; 
    } 

    public override int GetHashCode() 
    { 
     return this.ID.GetHashCode(); 
    } 
} 
0

我的第一個建議是將你的函數的名字改成AreFieldValuesTheSame。我的第二個建議是否定功能的目的。換句話說,當值相同時返回True。

如果你按照我的前兩個建議,那麼對於你的情況下每個人,而不是簡單地

Return (Nullable.Equals(a.ID, b.ID)) 

你需要的東西更像

Return ((a Is Nothing And b Is Nothing) Or (a.ID Is Nothing And b.ID Is Nothing) Or (Nullable.Equals(a.ID, b.ID))) 

例如,代表NewCasualty.Rank b表示CurrentCasualty.Rank。在嘗試檢查對象的屬性之前,您需要檢查對象是否爲Nothing(換言之,爲空)。

此外,在功能的開始,你需要檢查是否NewObj爲Nothing以及是否CurrentObj事在人爲:

If (NewObj Is Nothing) And (CurrentObj Is Nothing) Then 
    Return True 
Else If (NewObj Is Nothing) Or (CurrentObj Is Nothing) 
    Return False 

請原諒我,如果我的VB語法是不完美的。 (我更流利的C#。)