Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

oop - C# Address Model - Concatenate Address1, Address2 etc into a computed field

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...