为什么列表比较失败,如果列表是(似乎是)相同的
本文关键字:列表 似乎是 比较 失败 如果 为什么 | 更新日期: 2023-09-27 18:19:06
我的主要目标是比较存储在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.");
我对编写和调试代码很陌生,但我设法获得了QuickWatch在vs中的一些见解。我将数据复制到单独的文件并比较它们-它们是相同的:
http://pastebin.com/KFDHpQkC和http://pastebin.com/4j1n1nPH
有什么线索吗?您需要重写Equals
来判断这两个产品是否等效。通常当我们覆盖Equals
,我们也覆盖GetHashCode
:为什么重要的是重写GetHashCode当Equals方法被覆盖?
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;
}
};
您必须重写Equals
和GetHashCode
方法。Equals
方法将第一个对象的属性与另一个对象的属性进行比较。