I have created a matching method which basically takes in a Person object, makes a call to an address service and then tries to do a match firstly by trying like for like and then if that does not work it concatenates the house name/number with the saon information (flat 1a etc) to see if that works.
If it does find a match I need to store that matched address in my variable matchingAddress and then process that further which is what the rest of the method does, I have excluded this for clarity.
Problem
I think I have a problem with my lamda statement, on the second IF with the person I am debugging with the if statement is returning true and I am dropping into the assignment code however it is being set to null even though the IF statement is meant to be checking that it is not null.
Can anyone explain what is going on here?
private void AttemptMatch(Person person, string username)
{
var postcode = person.Postcode;
List<JsonAddressModel> Addresses = _service.GetAddress(postcode); //returns a list of addresses for that persons postcode
JsonAddressModel matchingAddress = new JsonAddressModel();
//1.) Access the Service and see if theres a straight forward match
if (Addresses.FirstOrDefault(x => x.paon.Trim() == person.AddressLineOne && x.thorofare.Trim() == person.AddressLineTwo) != null)
{
matchingAddress = Addresses.First(x => x.paon.Trim() == person.AddressLineOne && x.thorofare.Trim() == person.AddressLineTwo);
}
//2.) try and combine the paon and saon and see if that matches address line one
if (Addresses.Where(x => String.Format("{0} {1}", x.paon.Trim(), x.saon.Trim()) == person.AddressLineOne && x.thorofare.Trim() == person.AddressLineTwo) != null)
{
matchingAddress = Addresses.Where(x => String.Format("{0} {1}", x.paon.Trim(), x.saon.Trim()) == person.AddressLineOne && x.thorofare.Trim() == person.AddressLineTwo).FirstOrDefault();
}
if (matchingAddress != null)
{
//rest of method to complete matching process for matched person
}
else
return;
}