c# - Passed check on if not null is still assigning as null -
i have created matching method takes in person
object, makes call address service , tries match firstly trying like , if not work concatenates house name/number saon
information (flat 1a etc) see if works.
if find match need store matched address in variable matchingaddress
, process further rest of method does, have excluded clarity.
problem
i think have problem lamda
statement, on second if
person debugging if statement returning true , dropping assignment code being set null though if
statement meant checking not null
.
can explain going on here?
private void attemptmatch(person person, string username) { var postcode = person.postcode; list<jsonaddressmodel> addresses = _service.getaddress(postcode); //returns list of addresses persons postcode jsonaddressmodel matchingaddress = new jsonaddressmodel(); //1.) access service , see if theres 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 , combine paon , saon , see if matches address line 1 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 complete matching process matched person } else return; }
stop repeating work you've done1, , remove of conditional logic:
//1.) access service , see if theres straight forward match //if (addresses.firstordefault(x => x.paon.trim() == person.addresslineone && x.thorofare.trim() == person.addresslinetwo) != null) //{ matchingaddress = addresses.firstordefault(x => x.paon.trim() == person.addresslineone && x.thorofare.trim() == person.addresslinetwo) ?? //} //2.) try , combine paon , saon , see if matches address line 1 //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(); //}
your current problem caused because in second if
, you're not calling selection method (like firstordefault
or tolist
, etc) , you're checking whether query null rather result.
1at moment, run query merely determine whether returns results, , run query again obtain results. then, potentially (if you'd not got second if
wrong) same trick second time, and use overwrite previous result boot, actual result wanted, believe. so, instead, run 1 query, , if returns null, use ??
move along , try second query.
Comments
Post a Comment