Created
June 5, 2014 09:26
-
-
Save hatelove/b9925b9f7cd230b6ed90 to your computer and use it in GitHub Desktop.
Tuple與匿名型別偷吃步的比較方式,只比較特定值
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Linq; | |
namespace CollectionAssertSample | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
var expected = new List<OwnExpenseHPatientDoctorViewModel>(); | |
expected.Add(new OwnExpenseHPatientDoctorViewModel() | |
{ | |
AccountsCost = 2000, | |
DoctorName = "徐勵生", | |
OwnId = "2014050001", | |
PaidCost = 700, | |
PatientName = "test", | |
UnpaidCost = 1200 | |
}); | |
expected.Add(new OwnExpenseHPatientDoctorViewModel() | |
{ | |
AccountsCost = 0, | |
DoctorName = "謝佳臻", | |
OwnId = "2014050001", | |
PaidCost = 100, | |
PatientName = "test", | |
UnpaidCost = 0 | |
}); | |
var actual = new List<OwnExpenseHPatientDoctorViewModel>(); | |
actual.Add(new OwnExpenseHPatientDoctorViewModel() | |
{ | |
AccountsCost = 2000, | |
DoctorName = "徐勵生", | |
OwnId = "2014050001", | |
PaidCost = 700, | |
PatientName = "test", | |
UnpaidCost = 1200 | |
}); | |
actual.Add(new OwnExpenseHPatientDoctorViewModel() | |
{ | |
AccountsCost = 0, | |
DoctorName = "謝佳臻", | |
OwnId = "2014050001", | |
PaidCost = 100, | |
PatientName = "test", | |
UnpaidCost = 0 | |
}); | |
//var actual = await service.GetOwnReport(StartDate, EndDate, "", "All"); | |
//Assert.IsTrue(expected.SequenceEqual(actual)); | |
//default compare by reference should be false | |
Assert.IsFalse(expected.SequenceEqual(actual)); | |
//正規作法 | |
Assert.IsTrue(expected.SequenceEqual(actual, new OwnExpenseHPatientDoctorViewModelEqualityComparer()), "IEqualityComparer比較集合是否相等"); | |
//Tuple比較,只比較特定property值 | |
var expectdTuple = expected.Select(x => Tuple.Create(x.AccountsCost, x.OwnId)).ToList(); | |
var actualTuple = actual.Select(x => Tuple.Create(x.AccountsCost, x.OwnId)).ToList(); | |
Assert.IsTrue(expectdTuple.SequenceEqual(actualTuple)); | |
//匿名型別比較,只比較特定property值 | |
var expectedAnonymousType = expected.Select(x => | |
new | |
{ | |
AccountsCost = x.AccountsCost | |
, | |
Id = x.OwnId | |
}); | |
var actualAnonymousType = actual.Select(x => | |
new | |
{ | |
AccountsCost = x.AccountsCost | |
, | |
Id = x.OwnId | |
}); | |
Assert.IsTrue(expectedAnonymousType.SequenceEqual(actualAnonymousType)); | |
} | |
} | |
internal class OwnExpenseHPatientDoctorViewModelEqualityComparer : IEqualityComparer<OwnExpenseHPatientDoctorViewModel> | |
{ | |
public bool Equals(OwnExpenseHPatientDoctorViewModel x, OwnExpenseHPatientDoctorViewModel y) | |
{ | |
var result = x.AccountsCost == y.AccountsCost | |
&& x.DoctorName == y.DoctorName | |
&& x.OwnId == y.OwnId | |
&& x.PaidCost == y.PaidCost | |
&& x.PatientName == y.PatientName | |
&& x.UnpaidCost == y.UnpaidCost | |
&& x.UnpBaidCost == y.UnpBaidCost; | |
return result; | |
} | |
public int GetHashCode(OwnExpenseHPatientDoctorViewModel obj) | |
{ | |
//拿來當hash key用的,例如Dictionary<TKey,TValue> 這個Tkey什麼時候叫做Key一樣 | |
return 0; | |
} | |
} | |
public class OwnExpenseHPatientDoctorViewModel | |
{ | |
public int AccountsCost { get; set; } | |
public string DoctorName { get; set; } | |
public string OwnId { get; set; } | |
public int PaidCost { get; set; } | |
public string PatientName { get; set; } | |
public int UnpaidCost { get; set; } | |
public int UnpBaidCost { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment