[英]Intersect two object lists on a common property and then compare a different property
[英]Compare two Lists<object> returning different results
我有一個叫做TestResult的類,它看起來像這樣:
public class TestResult : IEquatable<TestResult> {
public TestResult(string labelName, List<object> correctValues) {
this.LabelName = labelName;
this.SelectedValues = correctValues;
}
public TestResult() {
}
public string LabelName { get; set; }
public List<object> SelectedValues { get; set; }
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
return obj.GetType() == GetType() && Equals((TestResult)obj);
}
public override int GetHashCode() {
unchecked {
int hashCode = this.LabelName.GetHashCode();
hashCode = (hashCode * 397) ^ this.SelectedValues.GetHashCode();
return hashCode;
}
}
public bool Equals(TestResult other) {
if (ReferenceEquals(null, other)) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
bool areEqual = false;
if (this.LabelName == other.LabelName) {
areEqual = true;
}
if (this.SelectedValues?.Count != other.SelectedValues?.Count) {
return false;
}
areEqual = this.SelectedValues.OrderBy(x => x).SequenceEqual(other.SelectedValues.OrderBy(x => x));
return areEqual;
}
/// <summary>
/// Override ==(you must ovverride this so if a developer called == it will return the same result as if they called Equals
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
/// <returns></returns>
public static bool operator ==(TestResult obj1, TestResult obj2) {
if (ReferenceEquals(obj1, obj2)) {
return true;
}
if (ReferenceEquals(obj1, null)) {
return false;
}
if (ReferenceEquals(obj2, null)) {
return false;
}
bool areEqual = false;
if (obj1.LabelName == obj2.LabelName) {
areEqual = true;
}
if (obj1.SelectedValues?.Count != obj2.SelectedValues?.Count) {
return false;
}
areEqual = obj1.SelectedValues.OrderBy(x => x).SequenceEqual(obj2.SelectedValues.OrderBy(x => x));
return areEqual;
}
/// <summary>
/// No need to repeat myself, just return the opposite of the == function
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
/// <returns></returns>
public static bool operator !=(TestResult obj1, TestResult obj2) {
return !(obj1 == obj2);
}
如您所見,我重寫了equals方法,以便在創建列表時可以比較對象。
然后,我有一個單元測試,該單元測試了我的equals方法,它看起來像這樣:
[TestMethod]
public void ReturnIncorrectTestResults_IncorrectValuesSubmitted_3LabelsWillBeReturned() {
List<string> failedLabelNames;
var submittedResults = new List<Repository.TestManagement.Models.TestResult> {
new Repository.TestManagement.Models.TestResult("Question1Label", new List<object> { true }),
new Repository.TestManagement.Models.TestResult("Question2Label", new List<object> { true }), //Difference
new Repository.TestManagement.Models.TestResult("Question3Label", new List<object> { 3, 4 }),
new Repository.TestManagement.Models.TestResult("Question4Label", new List<object> { true }),
new Repository.TestManagement.Models.TestResult("Question5Label", new List<object> { 1, 3 }), //Difference
new Repository.TestManagement.Models.TestResult("Question6Label", new List<object> { 1, 2, 3, 4 }),
new Repository.TestManagement.Models.TestResult("Question7Label", new List<object> { 1, 2, 3 }),
new Repository.TestManagement.Models.TestResult("Question8Label", new List<object> { 2 }),
new Repository.TestManagement.Models.TestResult("Question9Label", new List<object> { 3 }), //Difference
new Repository.TestManagement.Models.TestResult("Question10Label", new List<object> { 1, 2, 3, 4, 5 })
};
var validResults = new List<Repository.TestManagement.Models.TestResult> {
new Repository.TestManagement.Models.TestResult("Question1Label", new List<object> { false }),
new Repository.TestManagement.Models.TestResult("Question2Label", new List<object> { true }),
new Repository.TestManagement.Models.TestResult("Question3Label", new List<object> { 3, 4 }),
new Repository.TestManagement.Models.TestResult("Question4Label", new List<object> { true }),
new Repository.TestManagement.Models.TestResult("Question5Label", new List<object> { 5,6 }),
new Repository.TestManagement.Models.TestResult("Question6Label", new List<object> { 1, 2, 3, 4 }),
new Repository.TestManagement.Models.TestResult("Question7Label", new List<object> { 1, 2, 3 }),
new Repository.TestManagement.Models.TestResult("Question8Label", new List<object> { 2 }),
new Repository.TestManagement.Models.TestResult("Question9Label", new List<object> { 1 }),
new Repository.TestManagement.Models.TestResult("Question10Label", new List<object> { 1, 2, 3, 4, 5 })
};
failedLabelNames = _iTestManager.ReturnIncorrectTestLabels(submittedResults, validResults);
Assert.IsTrue(failedLabelNames.Count == 3);
}
因此,我的應用程序代碼中也有一個方法調用相同的equals函數:
public List<string> ReturnIncorrectTestLabels(List<TestResult> submittedResults, List<TestResult> acceptedResults) {
if (submittedResults.Count != acceptedResults.Count)
throw new ArgumentException($"The submitted results count is {submittedResults.Count} and the accepted results count is {acceptedResults.Count}. Amount of results must be equal.");
/*Compare the valid results against the submitted results. We join on the label names and
compare the results. Please not that this works because I have overridden the equals in
the TestResult class*/
var failedResultLabelNames = (from accepted in acceptedResults
join submitted in submittedResults
on accepted.LabelName equals submitted.LabelName
where accepted != submitted
select accepted?.LabelName).ToList();
return failedResultLabelNames;
}
我用它來比較兩個結果列表並返回任何失敗的值。
奇怪的是,我的單元測試通過了,但是當我在站點中進行測試時,它返回false,並且對象不相等。
因此,例如,如果我提交兩個看起來像這樣的列表:
var list1 = new List<TestResult> {
new TestResult("Question1Label", new List<object> { 1,2,3 }),
new TestResult("Question2Label", new List<object> { 4,5,6 })
};
var list2 = new List<TestResult> {
new TestResult("Question1Label", new List<object> { "1","2","3" }),
new TestResult("Question2Label", new List<object> { "4","5","6" })
};
我為我的兩個列表調用ReturnIncorrectTestLabels方法,它將兩個列表項都返回為“失敗”。
為什么會這樣呢?
public static bool operator ==(TestResult obj1, TestResult obj2) {
if (ReferenceEquals(obj1, obj2)) {
return true;
}
if (ReferenceEquals(obj1, null)) {
return false;
}
if (ReferenceEquals(obj2, null)) {
return false;
}
bool areEqual = false;
if (obj1.LabelName == obj2.LabelName) {
areEqual = true;
}
if (obj1.SelectedValues?.Count != obj2.SelectedValues?.Count) {
return false;
}
//Order to make sure that they are in correct order to be compared
obj1.SelectedValues = obj1.SelectedValues.OrderBy(x => x).ToList();
obj2.SelectedValues = obj2.SelectedValues.OrderBy(x => x).ToList();
for (int i = 0; i < obj1.SelectedValues.Count; i++) {
var type = obj1.SelectedValues[i].GetType();
//Use a dynamic so I can cast to the correct types at run time and compare
dynamic castedObj1Val = Convert.ChangeType(obj1.SelectedValues[i], type);
dynamic castedObj2Val = Convert.ChangeType(obj2.SelectedValues[i], type);
if (castedObj1Val != castedObj2Val)
return false;
}
return areEqual;
}
我在比較兩種不同的類型,因此在比較之前必須將它們轉換為正確的類型
這是因為!=
的執行方式類似於交叉聯接,因此將所有內容與所有內容進行比較,因此,將第一個項目與第二個項目進行比較時,您將獲得failedResult;而將第二個項目與第一項進行比較時,您還將獲得一個failedResult。
這是因為你在加入LabelName
是在兩個項目同list1
和list2
。 嘗試比較標簽名稱何時唯一(如它們在單元測試中一樣),它應該給出預期的結果。
var list1 = new List<TestResult> {
new TestResult("1", new List<object> { 1,2,3 }),
new TestResult("2", new List<object> { 4,5,6 })
};
var list2 = new List<TestResult> {
new TestResult("1", new List<object> { 1,2,3 }),
new TestResult("2", new List<object> { 4,5,6 })
};
var test = ReturnIncorrectTestLabels(list1, list2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.