gpt4 book ai didi

c# - 为什么列表比较失败,如果列表(似乎)相同

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:43 28 4
gpt4 key购买 nike

我的主要目标是比较存储在数据库和 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!");

Passes just fine!

但以下两项中的任何一项均未通过:

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/KFDHpQkChttp://pastebin.com/4j1n1nPH

有什么线索吗?

最佳答案

您需要覆盖 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;
}
};

关于c# - 为什么列表比较失败,如果列表(似乎)相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318065/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com