C# 比较两个强类型列表
C# Compare two strongly typed list
我有强烈的数据列表,我想找出数据中的差异。我已经使用了 EXCEPT,但我得到了我只希望收到的所有结果
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail889@hotmail.com", IsEmailValid = false, ValidityChecked = true });
我想达到以下几点;
1- 两个列表数据是否相等?
2- 记录差异
电子邮件Class
public class EmailValidationDto
{
public string EmailAddress { get; set; }
public bool IsEmailValid { get; set; }
public bool ValidityChecked { get; set; }
}
列表
var expectedEmailValidationDtoList = new List<EmailValidationDto>();
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "Myemail@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "TestEmail009@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "devOps@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail2@hotmail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail3.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "ccccc.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail889@hotmail.com", IsEmailValid = false, ValidityChecked = true });
var expectedEmailValidationDtoList2 = new List<EmailValidationDto>();
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "khurram@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "TestEmail009@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "devOps@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail2@hotmail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail3.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "ccccc.com", IsEmailValid = false, ValidityChecked = true });
var d1 = expectedEmailValidationDtoList.Except(expectedEmailValidationDtoList2).ToList();
var d2 = expectedEmailValidationDtoList2.Except(expectedEmailValidationDtoList).ToList();
您可以使用如下所示的 Where() 和 Any() 来比较 2 个列表:
var d1 = expectedEmailValidationDtoList1
.Where(email1 => !expectedEmailValidationDtoList2
.Any(email2 => email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
var d2 = expectedEmailValidationDtoList2
.Where(email1 => !expectedEmailValidationDtoList1
.Any(email2 => email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
或使用 All()
var d21 = expectedEmailValidationDtoList1
.Where(email1 => expectedEmailValidationDtoList2
.All(email2 => !email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
var d22 = expectedEmailValidationDtoList2
.Where(email1 => expectedEmailValidationDtoList1
.All(email2 => !email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
我有强烈的数据列表,我想找出数据中的差异。我已经使用了 EXCEPT,但我得到了我只希望收到的所有结果
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail889@hotmail.com", IsEmailValid = false, ValidityChecked = true });
我想达到以下几点;
1- 两个列表数据是否相等? 2- 记录差异
电子邮件Class
public class EmailValidationDto
{
public string EmailAddress { get; set; }
public bool IsEmailValid { get; set; }
public bool ValidityChecked { get; set; }
}
列表
var expectedEmailValidationDtoList = new List<EmailValidationDto>();
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "Myemail@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "TestEmail009@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "devOps@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail2@hotmail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail3.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "ccccc.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail889@hotmail.com", IsEmailValid = false, ValidityChecked = true });
var expectedEmailValidationDtoList2 = new List<EmailValidationDto>();
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "khurram@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "TestEmail009@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "devOps@hotmail.com", IsEmailValid = true, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail2@hotmail", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "testemail3.com", IsEmailValid = false, ValidityChecked = true });
expectedEmailValidationDtoList2.Add(new EmailValidationDto { EmailAddress = "ccccc.com", IsEmailValid = false, ValidityChecked = true });
var d1 = expectedEmailValidationDtoList.Except(expectedEmailValidationDtoList2).ToList();
var d2 = expectedEmailValidationDtoList2.Except(expectedEmailValidationDtoList).ToList();
您可以使用如下所示的 Where() 和 Any() 来比较 2 个列表:
var d1 = expectedEmailValidationDtoList1
.Where(email1 => !expectedEmailValidationDtoList2
.Any(email2 => email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
var d2 = expectedEmailValidationDtoList2
.Where(email1 => !expectedEmailValidationDtoList1
.Any(email2 => email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
或使用 All()
var d21 = expectedEmailValidationDtoList1
.Where(email1 => expectedEmailValidationDtoList2
.All(email2 => !email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));
var d22 = expectedEmailValidationDtoList2
.Where(email1 => expectedEmailValidationDtoList1
.All(email2 => !email1.EmailAddress
.Equals(email2.EmailAddress, StringComparison.InvariantCultureIgnoreCase)));