データの整合性があまり高くないデータベースからアドレスデータをインポートしようとしています。そのため、郵便番号がなく、NULLとして読み込まれている住所が(米国でも)多数あります。
これらのアドレスを既存のクリーンアドレスデータベースと照合しようとしています。宛先(会社名)、州(地区)、市(地方)、およびStreet1または郵便番号の最初の5つのいずれかに基づいて一致を決定しています。
私はこれを試しました:
//This is just coded for the example -- In my routine, potentialAddress
//is coming from a data source where Postal Code may or may not be null.
Address potentialAddress = new Address() {
Street1 = "2324 Lakeview Drive",
PostalCode = null,
CountryId = 234, //US
Locality = "Sanford",
District = "FL"
};
//What I want here is Country & District have to match and either
//Street matches OR (if this is a US address) the first 5 of the postal code matches
_context.Addresses.Where(a => ((a.Street1 == potentialAddress.Street1)
|| (a.PostalCode != null && potentialAddress.PostalCode != null && potentialAddress.PostalCode.SubString(0,5) == a.PostalCode.SubString(0,5))
&& a.CountryId == potentialAddress.CountryId
&& a.District == potentialAddress.District).Select(a => a.Id).ToList();
PotentialAddressがnullの場合は常にエラーメッセージが表示されます。私が得ている:
Object reference not set to an instance of an object
クエリジェネレータがpotentialAddress.SubString(..)を解析しようとしたとき。
どちらか一方(または両方)がnullの場合、郵便番号による一致とは呼びたくありません。
何か案は?