I have an address model in my application:
**[Key]**
public int CustomerAddressId { get; set; }
public string CompanyName { get; set; }
public string Address1 { get; set; } **NOT NULL**
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string Postcode { get; set; } **NOT NULL**
**[ForeignKey]**
public int CountryId { get; set; }
I have a Country model with CountryId, CountryNameCustomer
I would like to add a computed field to my customer address model which outputs a single address line, taking into account any fields which may be NULL.
For example, for the following address:
Address 1: 4 Raymond Street
Address 2: Leyton
City: London
Postcode: W13 5TY
I would like my computed field to be "4 Raymond Road, Leyton, London, W13 5TY".
I am not sure of the most elegant way to achieve this.
I could do something like the following:
var address = "";
if(CompanyName != null){address += CompanyName + ",";}
if(Address1 != null){address += Address1 + ",";}
and so on...
Is there a more elegant way to achieve this?
All the best.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…