How to write following if condition clean/fast/readable.
Any one of the fields 'address', 'city', 'district', 'province' can be null. I want to make full address by concat all these fields. Like
address = "Street # 32"
city = "My City"
District = "New York"
Province = "NY"
fullAddress = "Stree #32, My City, New York, NY"
If any information is missing then don't add this in string like if district is missing then don't include it.
address = "Street # 32"
city = "My City"
District = ""
Province = "NY"
fullAddress = "Stree #32, My City, NY"
Following code I have written but not good.
string address = dr["Address"].ToString();
string city = dr["City"].ToString();
string district = dr["District"].ToString();
string province = dr["Province"].ToString();
string fullAddress = "";
if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(city) && !string.IsNullOrEmpty(district) && !string.IsNullOrEmpty(province))
{
fullAddress = address + ", " + city + ", " + district + ", " + province;
}
else if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(city) && !string.IsNullOrEmpty(district))
{
fullAddress = address + ", " + city + ", " + district;
}
else if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(city) && !string.IsNullOrEmpty(province))
{
fullAddress = address + ", " + city + ", " + province;
}
else if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(district) && !string.IsNullOrEmpty(province))
{
fullAddress = address + ", " + district + ", " + province;
}
else if (!string.IsNullOrEmpty(city) && !string.IsNullOrEmpty(district) && !string.IsNullOrEmpty(province))
{
fullAddress = city + ", " + district + ", " + province;
}
else if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(city))
{
fullAddress = address + ", " + city;
}
else if (!string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(province))
{
fullAddress = address + ", " + province;
}
else if (!string.IsNullOrEmpty(address))
{
fullAddress = address;
}
else if (!string.IsNullOrEmpty(city))
{
fullAddress = city;
}