문제

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?

도움이 되었습니까?

해결책

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("?");
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top