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?

Was it helpful?

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("?");
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top