简体   繁体   中英

C# Compare two strongly typed list

I have strongly list with data and I want to find the difference in data. I have used EXCEPT but I am getting all the result where I only expect to receive

expectedEmailValidationDtoList.Add(new EmailValidationDto { EmailAddress = "testemail889@hotmail.com", IsEmailValid = false, ValidityChecked = true });

I want to achieve the following points;

1- If both list data equal or not? 2- the difference in records

Email Class

public class EmailValidationDto
{   
    public string EmailAddress { get; set; }
    public bool IsEmailValid { get; set; }
    public bool ValidityChecked { get; set; }
}

List

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();

You can use the Where() and Any() like below for comparing the 2 lists:

    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)));

Or use the 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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM