You could create an extension method:
public static string NullIf(this string str, string nullMarker)
{
return str == nullMarker ? null : str;
}
then you can do:
destImp.Cap = addr.location.postcode.NullIf("?");
...
Question
I'm doing this in my code:
destImp.Cap = (addr.location.postcode != "?") ? addr.location.postcode : null;
destImp.Civico = (addr.location.street != "?") ? addr.location.street : null;
destImp.Localita = (addr.location.city != "?") ? addr.location.city : null;
destImp.Indirizzo = (addr.location.street != "?") ? addr.location.street : null;
But it's cumbersome and redundant. There is a batter way to achieve the same result but with a better syntax?
Solution
You could create an extension method:
public static string NullIf(this string str, string nullMarker)
{
return str == nullMarker ? null : str;
}
then you can do:
destImp.Cap = addr.location.postcode.NullIf("?");
...