2015-10-24 64 views
1

我的主要目標是比較存儲在數據庫和XLSX文件中的數據。爲什麼列表比較失敗,如果列表(似乎是)相同

爲了做到這一點,我已經創建了兩個名單如下方式:

private class ProductList 
{ 
    public string productSku { get; set; } 
    public string productName { get; set; } 
    public string productSubfamilyId { get; set; } 
    public string productSubfamilyName { get; set; } 
    public string productFamilyId { get; set; } 
    public string productFamilyName { get; set; } 
}; 

(...) 
List<ProductList> productListsDB = new List<ProductList>(); 
List<ProductList> productListsXLSX = new List<ProductList>(); 
(...) 

要第一個,我提供直接從SQL查詢結果數據:

while (reader.Read()) 
{ 
    ProductList pl = new ProductList(); 
    pl.productSku = reader.GetString(reader.GetOrdinal("ProductSku")); 
    pl.productName = reader.GetString(reader.GetOrdinal("ProductName")); 
    pl.productSubfamilyId = reader.GetString(reader.GetOrdinal("ProductSubfamilyId")); 
    pl.productSubfamilyName = reader.GetString(reader.GetOrdinal("ProductSubfamilyName")); 
    pl.productFamilyId = reader.GetString(reader.GetOrdinal("ProductFamilyId")); 
    pl.productFamilyName = reader.GetString(reader.GetOrdinal("ProductFamilyName")); 
    productListsDB.Add(pl); 
} 

另外一個充滿了存儲數據在XLSX文件:

for (int rowNum = startingRow; rowNum <= totalRows; rowNum++) 
{ 
    var row = myWorksheet.Cells[rowNum, 1, rowNum, totalColumns].ToArray(); 

    ProductList pl = new ProductList(); 
    pl.productSku = (string)row[0].Value; 
    pl.productName = (string)row[1].Value; 
    pl.productSubfamilyId = (string)row[2].Value; 
    pl.productSubfamilyName = (string)row[3].Value; 
    pl.productFamilyId = (string)row[4].Value; 
    pl.productFamilyName = (string)row[5].Value; 
    productListsXLSX.Add(pl); 
} 

然後我想對它們進行比較和:

Assert.IsTrue(Equals(productListsDB.Count,productListsXLSX.Count), "Number of records in Excel file and DB differs!"); 

通過就好!

但是,任何兩個以下不及格:

Assert.IsTrue(productListsDB.All(productListsXLSX.Contains), "Data sent in Excel file and stored in DB are equal."); 
CollectionAssert.AreEquivalent(productListsDB, productListsXLSX, "Data sent in Excel file and stored in DB are equal."); 

我是很新,編寫和調試代碼,但我設法得到一些見解,在VS.與快速監視列表我複製的數據單獨的文件,並擔任司儀 - 他們是相同

http://pastebin.com/KFDHpQkChttp://pastebin.com/4j1n1nPH

任何線索的傢伙?

+0

爲什麼SKU的類名爲ProductList? – GolezTrol

+0

無關,名稱取自TC –

+1

只是關於代碼可讀性的評論,而不是核心問題。 – GolezTrol

回答

2

您需要覆蓋Equals來判斷2種產品是否相同。通常,當我們覆蓋Equals,我們也覆蓋GetHashCodeWhy is it important to override GetHashCode when Equals method is overridden?

private class ProductList 
{ 
    public string productSku { get; set; } 
    public string productName { get; set; } 
    public string productSubfamilyId { get; set; } 
    public string productSubfamilyName { get; set; } 
    public string productFamilyId { get; set; } 
    public string productFamilyName { get; set; } 

    public override bool Equals(object otherProduct) 
    { 
     //your code goes here to tell when the 2 products are equivalent. 
     //Here I assume that your 2 products are equal when all the properties are equal: 
     if (otherProduct == null) 
      return false; 
     return this.productSku == otherProduct.productSku && 
       this.productName == otherProduct.productName && 
       this.productSubfamilyId == otherProduct.productSubfamilyId && 
       this.productSubfamilyName == otherProduct.productSubfamilyName && 
       this.productFamilyId == otherProduct.productFamilyId && 
       this.productFamilyName == otherProduct.productFamilyName; 
    } 

    public override int GetHashCode() 
    { 
     //return your hash code 
     int hash = 13; 
     hash = (hash * 7) + this.productSku.GetHashCode(); 
     hash = (hash * 7) + this.productName.GetHashCode(); 
     ... 
     return hash; 
    } 
}; 
+0

謝謝!很好的解釋。你能幫我用GetHashCode方法嗎?從http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden,我應該如何獲得「field1」和「field2」?我應該在那裏創建ProductList對象嗎? –

+1

@BlackHat:查看我更新的答案。你不應該創建一個ProductList對象,只需從當前對象('this')計算它 –

+0

我們不是在那裏覆蓋那個哈希變量嗎?我們不應該在接下來的幾行中加上它嗎? –

1

你必須重寫EqualsGetHashCode方法。 Equals方法會將第一個對象的屬性與另一個對象屬性進行比較。

+0

覆蓋Equals僅僅用於測試目的是代碼異味。改用相等比較器。 –