我的主要目标是比较存储在DB和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中的QuickWatch获得了该列表的一些见解。 我将数据复制到单独的文件并对其进行了编辑 - 它们是相同的:
http://pastebin.com/KFDHpQkC 和 http://pastebin.com/4j1n1nPH
任何线索的家伙?
答案 0 :(得分:2)
您需要覆盖Equals
来判断这两种产品是否相同。通常,当我们覆盖Equals
时,我们也会覆盖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;
}
};
答案 1 :(得分:1)
您必须覆盖Equals
和GetHashCode
方法。 Equals
方法将第一个对象的属性与另一个对象属性进行比较。