Reputation: 741
my main target was to compare data stored in DB and in XLSX file.
To do that, I have two lists created following way:
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>();
(...)
To first one, I provide data directly from SQL query result:
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);
}
Another one is filled with data stored in XLSX file:
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);
}
Then I wanted to compare them and:
Assert.IsTrue(Equals(productListsDB.Count,productListsXLSX.Count), "Number of records in Excel file and DB differs!");
Passes just fine!
But any of two following does NOT pass:
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.");
I'm pretty new to writing and debugging code, but I managed to get some insights of that lists with QuickWatch in VS. I copied data to separate files and compered them - they are identical:
http://pastebin.com/KFDHpQkC and http://pastebin.com/4j1n1nPH
Any clues guys?
Upvotes: 1
Views: 70
Reputation: 48972
You need to override Equals
to tell whether the 2 products are equivalent. Usually when we override Equals
, we also override GetHashCode
: Why 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;
}
};
Upvotes: 2
Reputation: 10839
You have to override the Equals
and GetHashCode
methods. The Equals
method will compare the property of first object to another object property.
Upvotes: 1