This post gives you a simple way of getting an address on one line, separated by commas and it ignores any blank lines.

public class Address
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string PostCode { get; set; }
    public string Country { get; set; }
public string OnOneLine => string.Join(", ", ((new List() { Line1, Line2, City, County, PostCode, Country })).Where(x => !string.IsNullOrEmpty(x))); }

So now you can test it like this.

Address test1 = new Address() { Line1 = "10 Street Name", Line2 = "Smallville", City = "Big City", County = "Shire", PostCode = "WC1 1DN", Country = "United Kingdom" };
Console.WriteLine(test1.OnOneLine);
//returns: 10 Street Name, Smallville, Big City, Shire, WC1 1DN, United Kingdom

Address test2 = new Address() { Line1 = "10 Street Name", City = "Big City", PostCode = "WC1 1DN", Country = "United Kingdom" };
Console.WriteLine(test2.OnOneLine);
//returns: 10 Street Name, Big City, WC1 1DN, United Kingdom

Address test3 = new Address() { Line1 = "10 Street Name", Line2 = "Smallville", PostCode = "WC1 1DN" };
Console.WriteLine(test3.OnOneLine);
//returns: 10 Street Name, Smallville, WC1 1DN

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to include scripts from partial views in MVC and Umbraco

This post will show you how you can include scripts from inside an MVC partial view rather than havi…

Read Post

How to fix the error "Authentication failed because the remote party has closed the transport stream"

This post will show you how to fix the error "Authentication failed because the remote party has clo…

Read Post

How to solve the error Could not load file or assembly System.Security.Principal.Windows

This post help you solve the error System.IO.FileNotFoundException Could not load file or assembly S…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post